Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapsV2 how to fake a click of the myLocation button

In MapsV2, I would like to programmatically perform a click of the myLocation button that appears on the top right of the map.

In previous versions of MapsV2, I could get a handle on the button by traversing the view hierarchy and then say myButton.performClick() (and also if I wanted could reposition the button on the map). But in the latest release this has stopped working so I think Google have deliberately disabled this potential.

How else can I center the map on the current location from code, as if the user had pressed the button?

like image 644
androidneil Avatar asked Dec 16 '13 18:12

androidneil


1 Answers

just use

map.getMyLocation()

which returns you the location of the blue dot then just change the camera to that location

CameraPosition position = new CameraPosition.Builder()
   .target(new LatLng(location.getLatitude(),location.getLongitude()))
   .zoom(zoom).build();

map.animateCamera(CameraUpdateFactory.newCameraPosition(position));

there is also a OnMyLocationButtonClickListener if you want to listen for the my location button click

like image 165
tyczj Avatar answered Sep 21 '22 19:09

tyczj