我有一个奇怪的问题。我正在编写一个使用Google地图框架的应用程序,由于某种奇怪的原因,标记可以完美移动,但是相机不能跟随它。这是我的onLocationChanged方法。
@Override
public void onLocationChanged(Location location) {
if (currentMarker != null) {
currentMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MarkerOptions options = new MarkerOptions();
options.position(latLng);
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
currentMarker = mMap.addMarker(options);
CameraUpdate center=
CameraUpdateFactory.newLatLng(latLng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15.0f);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
if(client != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
}
}
再次,我一生无法理解问题所在
zoom
CameraUpdate
包含您实际移动相机的位置之前,因此mMap.animateCamera(zoom)
只是将其从前一个moveCamera
移回。要解决,请尝试:
CameraUpdate center= CameraUpdateFactory.newLatLngZoom(latLng,15.0f);
mMap.animateCamera(center);
或
CameraUpdate center = CameraUpdateFactory.newLatLng(latLng);
mMap.moveCamera(center);
CameraUpdate zoom= CameraUpdateFactory.zoomTo(15.0f);
mMap.animateCamera(zoom);