当我打开我的应用程序并且它没有添加标记时,Camera Animation无法指向我的位置
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
这里它不会将我的相机指向我当前的位置。当我打开我的应用程序时,它从地图的中心开始而不是我的位置
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMapIfNeeded();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
LatLng target = new LatLng(location.getLatitude(), location.getLongitude());
CameraPosition position = this.mMap.getCameraPosition();
CameraPosition.Builder builder = new CameraPosition.Builder();
builder.zoom(15);
builder.target(target);
this.mMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("You are here!").snippet("Consider yourself located"));
}
}
}
答案 0 :(得分:5)
你有几个巨大的超级基本错误:
只有当baground中的活动到达前台时才会调用onResume
。将setUpMapIfNeeded()
放在onCreate
内。
在上一个setUpMapIfNeeded()
语句中,您的函数if
中的第二个而不是setUpMap()
您将setUpMapIfNeeded()
更改为public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
,它应该有效。
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
以下是您修改的代码:
第1部分
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new.MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
LatLng target = new LatLng(location.getLatitude(), location.getLongitude());
CameraPosition position = this.mMap.getCameraPosition();
CameraPosition.Builder builder = new CameraPosition.Builder();
builder.zoom(15);
builder.target(target);
this.mMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("You are here!").snippet("Consider yourself located"));
}
}
}
第2部分
CAGradientLayer
答案 1 :(得分:1)
Sub mySub()
On Error GoTo CatchError 'I called it CatchError but you can call it whatever
'--All of your code here including "If MsgBox("File has been updated", vbinformatoin) = ok Then Exit Sub"
CatchError:
'Here put what you want to do in case of an error, for example
'MsgBox "The code stopped because of an error"
End Sub
只需改变这两种方法