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

mai*_*iky 11 google-maps bounds google-maps-api-3

我有一个点(X,Y),我想创建一个正方形,谷歌映射LatLngBounds对象,以使地理编码请求只偏向此LatLngBound区域.

如何创建具有给定点中心的LatLngBounds方形?我必须找到NE和SW点.但是如果给定距离d和点(x,y),我怎么能找到它呢?

谢谢

wpr*_*ter 37

您也可以getBounds从定义为圆的半径中将trig保留为google.

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

  • 谢谢!这对我有用:`var latLng = new google.maps.LatLng(32.79503,-117.24142);/*San Diego*/var radius = 2000;/*米*/var circle = new google.maps.Circle({center :latLng,radius:radius}); var defaultBounds = circle.getBounds();` (5认同)

her*_*ist 6

那很复杂.对于一个粗糙的盒子试试这个:

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);
Run Code Online (Sandbox Code Playgroud)

编辑

以上是他们在2011年写这篇文章时的方式.这些天谷歌地图api已经成为一个漫长的方式.@wprater的答案更简洁,并使用了一些较新的api方法.

  • 好的,但解释一下有关代码的一些话,以便其他观众能够理解.什么是变量和它做什么,当插入10作为dist时,它是公里?当你把NE角落里的45,10和SW角落里的225,10这个是什么意思? (2认同)