简体   繁体   中英

Pan first, then zoom to current location in Android

Starting out with Android, I am using a very slightly modified version of the Google Maps API demo. The default behavior on clicking the MyLocationButton is that Android Pans to the current location. I just added a line to zoom in.

@Override
public boolean onMyLocationButtonClick() {
    Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
    // Return false so that we don't consume the event and the default behavior still occurs
    // (the camera animates to the user's current position).
    float zoomLevel = (float) 17.0;
    mMap.moveCamera(CameraUpdateFactory.zoomTo(zoomLevel));
    return false;
}

This works, the app zooms to the indicated level then pans to the location. But I would prefer to Pan first , then Zoom. As I see it, I can accomplish this by either

  1. Accessing the current location from within the onMyLocationButtonClick anonymous function, or
  2. Running the code after the default behavior occurs.

Sadly, I don't know how to do either of those things.

1) you cant look into the "anonymous" function, but you know what it does. What you need is your current location, you can take a look into getting the last known location , and then, when your button is clicked, you move/"animate" the camera to the LatLng, and after it use your .zoomTo. dont forget to change ur returningvalue to return true; so your locationbuttonclick event is not passed to this 'anonymous' function amymore.

2) instead of implementing a locationmanager which would be necessary for step (1), you can add a camerachangeListener to your map and add ur zoomTo method everytime, after the cameraposition has changed. When your button is clicked, it will automatically trigger that onCameraChange event and then zoom in. here some sample code:

mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            // (the camera animates to the user's current position).
            float zoomLevel = (float) 17.0;
            if (buttonClicked){
                mMap.moveCamera(CameraUpdateFactory.zoomTo(zoomLevel));
                buttonClicked = false;
            }
        }
    });

where mMap is your googlemap inside onMapReady... Unfortunetly, your camera would always zoom in, if the camera changes, eg when your user scrolls the map. In order to prevent that, you could use a boolean class-variable buttonClicked to indicate when your button is clicked:

@Override
public boolean onMyLocationButtonClick() {
     Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
     buttonClicked = true;
     return false;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM