0

setLocationEnabled is a attribute that shows a button on the top of the map that makes the map go to the current place.

But I want to "hide" this Button and call the method on the another custom button. Is there a way? I don't wanna to create a big code to go to current location, it's not fast. I just wanna a button with the same function of the setLocationEnabled(true).

How do I do that?

Teste
  • 661
  • 1
  • 6
  • 7

2 Answers2

0

I've dealt with a similar problem and I found a pretty detailed solution from here Google map for android my location custom button

Have fun!

Community
  • 1
  • 1
Gerrerth
  • 63
  • 1
  • 4
0

As you may know, you can hide the current location button with the following:

UiSettings.setMyLocationButtonEnabled(false);

Now, if you want to perform this task manually, you'll have to do some work on your own. First, you will need to acquire the user's current location. This can be acquired (and updated) by overriding the onLocationChanged function of the LocationListener implementation. If you are unclear on how to do this, checkout the documentation describing it. This may prove useful as well!

Secondly, you'll need to target the user's location by setting the map's camera to look at it, and (possibly) set a marker in the location. Here's the function I used in one of my recent apps describing how to do so:

public void moveToLocation(Location input) {
    Double lat = (double) (input.getLatitude());
    Double lon = (double) (input.getLongitude());

    final LatLng location = new LatLng(lat, lon);

    setLocMarker(location);

    mainMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 16));

    //if you want the camera to have an "animated" effect, you can perform the following
    //mainMap.animateCamera(CameraUpdateFactory.zoomTo(16), 4000, null);
}

For more information regarding camera effects and animations, refer to this documentation.

Shane Duffy
  • 1,117
  • 8
  • 18