简体   繁体   中英

Android: how to check if myLocation is visible on the map at the current zoom level?

I have a Google map where as the user moves around the blue dot (myLocation) moves - standard behaviour. This works well until the user moves beyond the bounds of the current zoom level - and the blue dot disappears off one of the edges of the map. I don't want to refocus the map every single time that the user moves and I get a location update - it isn't necessary for the myLocation dot to always be in the center of the screen - but it is necessary for the myLocation to always be visible, so if the user moves off one side of the map I want to refocus the map so that he becomes visible again.

How can I tell that the myLocation has moved out of the visible area of the screen?

 double latitude = location.getLatitude();
 double longitude = location.getLongitude();
 fromPosition = new LatLng(latitude, longitude);

 LatLngBounds bounds = this.map.getProjection().getVisibleRegion().latLngBounds;       
 if(!bounds.contains(new LatLng(location.getLatitude(), location.getLongitude()))){
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(fromPosition)      
                    .zoom(17)                   
                    .bearing(180)                
                    .tilt(80)                  
                    .build(); 
            map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
 }

Solved by getting current bounds for the map and then checking "contains" on the last known latlng:

if (!map.getProjection().getVisibleRegion().latLngBounds.contains(locationToLatLng(lastKnownLocation))) {
                map.moveCamera(CameraUpdateFactory.newCameraPosition(getCameraPositionFromLocationWithZoom(lastKnown, getCurrentZoom())));
            }

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