繁体   English   中英

使用google maps api v3查找给定其中心点的地图边界

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

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

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM