Reputation: 8021
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?
Upvotes: 3
Views: 2650
Reputation:
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));
}
Upvotes: 6
Reputation: 8021
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())));
}
Upvotes: 1