简体   繁体   中英

Follow my location if user does not update camera manually - Google maps v2

I am working with google maps in my Android app. I want to animate camera inside onLocationChange by default and every time the user clicks My location button. But if the user moves camera manually, I want to stop updating camera to current location. Actually, I am always updating the camera:

@Override
public void onLocationChanged(Location location) {
   googleMap.animateCamera(map.getCamera(location));
}

Maybe using a boolean var but I'm not sure how.

You are on the right track.

Define:

private boolean followUser;

add check:

@Override
public void onLocationChanged(Location location) {
    if (followUser) {
        googleMap.animateCamera(map.getCamera(location));
    }
}

in onMyLocationButtonClick :

followUser = true;

and last thing, you need to make sure when user starts interacting with the map, you stop following, so add an overlay eg:

<FrameLayout>
    <MapView />
    <View />
</FrameLayout>

and add OnTouchListener to that View , where you do:

followUser = false;
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