Google maps 谷歌地图。如何在给定中心点坐标的情况下创建LatLngBounds矩形(正方形)

Google maps 谷歌地图。如何在给定中心点坐标的情况下创建LatLngBounds矩形(正方形),google-maps,google-maps-api-3,bounds,Google Maps,Google Maps Api 3,Bounds,我有一个点(X,Y),我想创建一个正方形的Google maps LatLngBounds对象,这样地理编码请求只会偏向这个LatLngBound区域 如何创建以给定点为中心的LatLngBounds正方形?我必须找到东北和西南点。但是如果给定距离d和点(x,y),我怎么能找到它呢 谢谢,那太复杂了。对于粗糙的盒子,请尝试以下方法: if (typeof(Number.prototype.toRad) === "undefined") { Number.prototype.toRad = f

我有一个点(X,Y),我想创建一个正方形的Google maps LatLngBounds对象,这样地理编码请求只会偏向这个LatLngBound区域

如何创建以给定点为中心的LatLngBounds正方形?我必须找到东北和西南点。但是如果给定距离d和点(x,y),我怎么能找到它呢


谢谢,那太复杂了。对于粗糙的盒子,请尝试以下方法:

if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

if (typeof(Number.prototype.toDeg) === "undefined") {
  Number.prototype.toDeg = function() {
    return this * 180 / Math.PI;
  }
}

var dest = function(lat,lng,brng, dist) {
    this._radius = 6371;
    dist = typeof(dist) == 'number' ? dist : typeof(dist) == 'string' && dist.trim() != '' ? +dist : NaN;
    dist = dist / this._radius;
    brng = brng.toRad();  
    var lat1 = lat.toRad(),
        lon1 = lng.toRad();

    var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
    var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1), Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2));
    lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
    return (lat2.toDeg() + ' ' +  lon2.toDeg());
}

    var northEastCorner = dest(centreLAT,centreLNG,45,10);
    var southWestCorner = dest(centreLAT,centreLNG,225,10);
编辑


以上是我在2011年写这篇文章时他们的做法。这些天来,谷歌地图api的发展非常缓慢。@wprater给出的答案要简洁得多,并且使用了一些较新的api方法。

您还可以
getBounds
从定义为圆的半径开始,将三角留给google


new google.maps.Circle({center:latLng,radius:radius}).getBounds()

简单地在x/y位置上加/减d/2不是很有效吗

以x,y为中心点:

NW = x-(d/2),y-(d/2)
SE = x+(d/2),y+(d/2)
不过,在这一点上不要相信我——我数学很差:)


这假定d为“直径”,而不是半径。如果“d”是半径,那么不用担心除以两部分。

好的,但要多解释一些关于代码的单词,以便其他观众能够理解。brng变量是什么?它的作用是什么?当插入10作为dist时,它是km?东北角45,10,西南角225,10是什么意思?ok
brng
是方位角或角度。因此,我们使用45,10计算起点东北10km处的一个点的位置。225,10将为您提供西南位置。这是google bounds对象所需的两点。对于那些最近否决了我的答案的人-检查答案的时间-2011年4月,当时使用
google.maps.Circle
的解决方案不可用。谢谢!这对我很有用:
var latLng=new google.maps.latLng(32.79503,-117.24142)/*圣地亚哥*/var半径=2000/*meters*/var circle=new google.maps.circle({center:latLng,radius:radius});var defaultBounds=circle.getBounds()