How to animate the camera to a specified location in Google Maps v2 for Android?
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(totalAddress); //Here Total Address is address which you want to show on marker
mMap.clear();
markerOptions.icon(
BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
markerOptions.getPosition();
mCurrLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
Whenever the user selects the option, you are using
LatLng initialLoc= mMap.getCameraPosition().target;
to get the supposedly initial location, which is wrong!
mMap.getCameraPosition().target returns the location that the camera is pointing at. You should store the lat long in the onCreate()
of the activity or some other place as per your other code, and then use the same in onOptionItemSelected()
.
Btw you can combine the zoom and lat long in a single statement as follows.
LatLng coordinate = new LatLng(lat, lng); //Store these lat lng values somewhere. These should be constant.
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(
coordinate, 15);
mMap.animateCamera(location);
UPDATE
I dont really know as to how accurate that'd be or when to call it. But you could use the same code
LatLng initialLoc= mMap.getCameraPosition().target;
Instead call this once in your onCreate()
, or onResume()
and then store it there. Then next time in your optionsItemSelected()
use those values. Although, why don't you simply store those values that you defined in your xml in java code and then use it?