[英]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. 我有一个谷歌地图,当用户围绕蓝点(myLocation)移动时 - 标准行为。 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. 我不希望每次用户移动时重新聚焦地图并且我得到位置更新 - myLocation点不必始终位于屏幕的中心 - 但是myLocation必须是永远是可见的,所以如果用户从地图的一侧移开,我想重新聚焦地图,以便他再次可见。
How can I tell that the myLocation has moved out of the visible area of the screen? 如何判断myLocation已移出屏幕的可见区域?
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: 通过获取地图的当前边界然后在最后已知的latlng上检查“包含”来解决:
if (!map.getProjection().getVisibleRegion().latLngBounds.contains(locationToLatLng(lastKnownLocation))) {
map.moveCamera(CameraUpdateFactory.newCameraPosition(getCameraPositionFromLocationWithZoom(lastKnown, getCurrentZoom())));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.