How to animate a camera at a specified location in Google Maps v2 for Android?

I'm having trouble moving the Google Maps camera to its original position. Basically, I have a home button on the action bar. If the user scrolls the map and presses the "Home" button, I would like the camera to return to its original location.

I get the source coordinates using googleMap.getCameraPosition().target , but the camera does not move.

 @Override public boolean onOptionsItemSelected(MenuItem item) { LatLng initialLoc= mMap.getCameraPosition().target; CameraUpdate update = CameraUpdateFactory.newLatLng(initialLoc); CameraUpdate zoom = CameraUpdateFactory.zoomTo(15); mMap.moveCamera(update); mMap.animateCamera(zoom); return true; } 

map.xml:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/map" tools:context="org.test" android:name="com.google.android.gms.maps.SupportMapFragment" map:cameraTargetLat="xxx.xxx" map:cameraTargetLng="xxx.xxx" map:cameraZoom="15" map:mapType="normal" /> </RelativeLayout> 

What am I missing?

+7
source share
2 answers

Whenever the user selects this option, you use

 LatLng initialLoc= mMap.getCameraPosition().target; 

to get the supposedly starting location, which is wrong! mMap.getCameraPosition (). target returns the location that the camera points to. You should store lat long in onCreate() activity or somewhere else according to your other code, and then use the same in onOptionItemSelected() .

Btw you can combine scale and lat in one expression 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 really don't know how accurate it would be or when it could be called. But you can use the same code

 LatLng initialLoc= mMap.getCameraPosition().target; 

Instead, call it once in onCreate() or onResume() , and then save it there. Then next time in optionsItemSelected() use these values. Although, why don't you just save the values ​​that you defined in your xml in java code and then use it?

+22
source
  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)); 
0
source

All Articles