使用googlemapsapiv3查找给定中心点的地图边界

find the bounds of my map given its center point with google maps api v3

本文关键字:地图 边界 中心点 googlemapsapiv3 查找 使用      更新时间:2023-09-26

我正在尝试确定正在设置动画的标记是否在地图的可视区域内。如果不是,我想告诉我的地图平移到标记位置作为中心位置。我该怎么做?这是我的代码:

var curPoint = racePath.GetPointAtDistance(curDist);
var curZoom = self.map.zoom;
var mapBounds = new google.maps.LatLngBounds(currentCenter);
var isInBounds = mapBounds.contains(curPoint);

这是不起作用的,因为mapBounds需要地图的西南点和东北点,但如果不从mapBounds.getNorthEast()和mapBounds.getSouthWest()方法中获取它们,我不知道如何计算这些点-这里有文档:https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds.

关于如何解决这个问题有什么想法吗?

要确定映射的当前边界,请在映射变量上调用getBounds():https://developers.google.com/maps/documentation/javascript/reference#Map

如果您想知道一个点是否在当前视图区域内,请在LatLngBounds上调用"contains(LatLng)"。像这样:

var currentBounds = myMap.getBounds();
var isInSight = currentBounds.contains(myPointLatLng);
if(isInSight){
    //Your point is within the current bounds
}

如果你想平移,使地图以你的点为中心,在你的地图变量上调用"panTo(LatLng)":

var currentBounds = myMap.getBounds();
var isInSight = currentBounds.contains(myPointLatLng);
if(isInSight){
    myMap.panTo(myPointLatLng);
}