Google Maps API v2 calls button click behavior mylocation

I have it fully working GoogleMap

and I can show my userlocation using a call

mMap.setMyLocationEnabled(true);

      

What I want to accomplish is the functionality of the actual button click that now appears on GoogleMap

(Ie animate the map to a custom location with the correct zoom, etc.).

The functionality already exists in the GoogleMap object, how to use it?

I don't want to use LocationListener

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

Thanks in advance.

EDIT:

What I basically want to do is center the map on the user's location, just like GoogleMap focuses on the user's location when I click a button. Like this:

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

      

EDIT:

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

+3


source to share


3 answers


Not really sure why the other answers are talking about OnMyLocationChangeListener

s. This is not what the OP asked. A question has been asked about how to copy the exact clicking behavior of the MyLocationButton, which is actually more difficult than just animating the camera to the user's location. The button performs different actions depending on which zoom level you are currently using.

For example, when you zoom in, clicking a button will animate your location while zooming, whereas if you have already reached a certain zoom level, clicking a button will only animate you to your location while maintaining the current zoom level.

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



@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();
}

      

Yes, it isn't, and relies on hardcoded identifiers that may change in future versions of the Google Maps API. But this method of getting the button is based on this highly acclaimed answer from 2013 and still, as of Google Play Services 8.4.0, still works flawlessly.

+2


source


This is how I do to go to the center of the map when we receive 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

+4


source


2013:

. OnMyLocationChangeListener. .

. , GoogleMap Android , . , ( , ).

LocationSource LocationListener LocationManager .

.

:

+1









All Articles