3

I have a fully working GoogleMap and I can show my userlocation with the call

mMap.setMyLocationEnabled(true);

What I want to accomplish is the functionality of the actual click on the button that now appears on the GoogleMap (I.e animating the map to the userlocation with correct zoom etc).

The functionality is there in the GoogleMap-object already, how do I use it?

I do not want to use a LocationListener or such to accomplish this, I just want to "call the same code" that gets called when I click the button on the map. Can it be that simple?

Thanks in advance.

EDIT:

What I basically want to do is to center the map at the user location, exactly the way that the GoogleMap centers at user location when I click the button. Like this:

    GoogleMap mMap = this.getMap();
    if(mMap != null) {
        mMap.setMyLocationEnabled(true);
        //TODO center the map on mylocation
    }

EDIT:

Apparently Google is working on this. http://code.google.com/p/gmaps-api-issues/issues/detail?id=4644

ullstrm
  • 9,812
  • 7
  • 52
  • 83
  • Could you add the code that you have used for maps. Would be easier to check that out. – lokoko Jan 30 '13 at 08:35
  • I've edited my original post with some code. Is this what you were looking for? – ullstrm Jan 30 '13 at 09:49
  • I meant the google maps file itself so that we could find out which is the component that would be used to center the map. – lokoko Jan 30 '13 at 09:57
  • You could also refer to this : http://stackoverflow.com/questions/3705336/google-maps-api-not-centering-the-marker?rq=1 – lokoko Jan 30 '13 at 09:57
  • Do you want to get some values we get when we touch or click the default current location button(or layer) embedded-in the right-top of Google maps? – BBonDoo Jan 30 '13 at 10:09
  • All I want to do is the exact same thing that gets called when you CLICK the button, but I want to do it programmatically. lokoko, that example is for javascript, I am working with android and the new google maps api v2. – ullstrm Jan 30 '13 at 12:17

3 Answers3

4

This is how I do to navigate to the center of the map when we get the first location-update.

my class header:

public class FragActivity extends SherlockFragmentActivity implements  OnMyLocationChangeListener

private GoogleMap mMap;

my mMap-setup:

    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = customMapFragment.getMap();

        // Check if we were successful in obtaining the map.
        if (mMap != null)
            setUpMap();
    }

setUpMap-method:

private void setUpMap() {
    mMap.setMyLocationEnabled(true);
    mMap.setOnMyLocationChangeListener(this);
}

and my onlocationchange:

@Override
public void onMyLocationChange(Location lastKnownLocation) {
    CameraUpdate myLoc = CameraUpdateFactory.newCameraPosition(
            new CameraPosition.Builder().target(new LatLng(lastKnownLocation.getLatitude(),
                    lastKnownLocation.getLongitude())).zoom(6).build());
    mMap.moveCamera(myLoc);
    mMap.setOnMyLocationChangeListener(null);
}

Works like a charm

ullstrm
  • 9,812
  • 7
  • 52
  • 83
  • 1
    for animation to location, use; mMap.animateCamera(myLoc); – ullstrm Feb 27 '13 at 13:23
  • setOnMyLocationChangeListener is now deprecated, see the following question: https://stackoverflow.com/questions/36444085/android-setonmylocationchangelistener-is-deprecated – simonarys Apr 27 '22 at 14:30
3

Not really sure why the other answers are talking about OnMyLocationChangeListeners. This is not what was asked in the OP. What was asked was how to replicate the exact behavior of clicking on the MyLocationButton, which is actually more complex than just animating the camera to the user's location. The button does different things depending on what zoom level you're currently on.

For example, when you're zoomed way out, clicking the button will animate you to your location while zooming you in, whereas if you're already at certain zoom levels, clicking the button will only animate you to your location while maintaining the current zoom level.

Rather than painstakingly trying to replicate this behavior, wouldn't it be great if we could simply get a reference to the MyLocationButton and just, you know, click it? Well, this is the current best way I've found to do just that:

@OnClick(R.id.fabMyLocation)
public void onMyLocationClick(FloatingActionButton fabMyLocation) {
    View myLocationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).getParent())
            .findViewById(Integer.parseInt("2"));

    myLocationButton.performClick();
}

Yeah, it's not pretty, and relies on hardcoded IDs that may change in future versions of the Google Maps API. But this method of retrieving the button is based off of this highly rated answer from 2013 and so far, as of Google Play Services 8.4.0, still works flawlessly.

Community
  • 1
  • 1
Aphex
  • 7,390
  • 5
  • 33
  • 54
1

Update February 2013:

This is now possible. You can now set the map's OnMyLocationChangeListener. Receive an update as you want and move the camera accordingly.

This is not possible. I've found that the GoogleMap in Android is quite limited in the information that it gives you. It simply doesn't give you direct access to the user location updates or to the button that the user can press (you can only enable/disable it, not simulate a press).

You must implement the LocationSource and LocationListener interfaces and use a LocationManager to get updates from the device and use them to send them to the map.

You could then add your own custom button to the overlay to replicate the default button and use your own camera and zoom controls to simulate the functionality.

Source: hours of searching

Rowan Freeman
  • 15,724
  • 11
  • 69
  • 100
  • 2
    This is what I've found. Apparently google is working on this. http://code.google.com/p/gmaps-api-issues/issues/detail?id=4644 – ullstrm Feb 06 '13 at 09:24