I enabled the my-location layer of the Google Maps Android API v2, which adds the floating button to go to the user's current location. I need a way to detect a click on this button. Is this possible?
4 Answers
Since this question was posted, Google has updated the API. They have added an onMyLocationButtonClick()
listener.
To add the listener:
//add location button click listener
map.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener(){
@Override
public boolean onMyLocationButtonClick()
{
//TODO: Any custom actions
return false;
}
});
Returning false will essentially call the super method. Returning true will not.
The latest release of Google Maps android api V2 in August contains a listener method for clicks of my location button : onMyLocationButtonClick () This can be used to know when the user has clicked the button and returns true if the listener has consumed the event
No, currently there is no way to get that information. There is a feature request raised for that and it is already Acknowledged by Google.
Star it if you want a quicker fix:
Workaround:
Disable the built-in control and create your own View and add it to the screen. Set an onClickListener to this button and animate the map to the map's location.
Disable the default control:
UiSettings.setMyLocationButtonEnabled(false)
Get my location from the map:
GoogleMap.getMyLocation()
-
5This is no longer true, Android has updated. See Tony's or my answer below.– CyberEdJun 17, 2015 at 16:02
This is good to know as I was also wondering about this. Now, is there a way to know whether or not Google's location service is enabled so we can decide whether or not we want to enable the button or not? No sense having a button that doesn't do anything so the workaround (disabling that button always) seems to be the only option for now.