谷歌地图得到标记周围的边界框

时间:2011-03-07 12:47:43

标签: javascript google-maps-api-2

在谷歌地图api v2中,我在地图上有一个标记,我需要计算一个标记围绕这个标记。我怎样才能得到一个5到5公里的标记为中心的粘接盒?

2 个答案:

答案 0 :(得分:2)

我不确定谷歌地图是否提供了这样的功能,但数学将帮助您生存;)Calculate distance, bearing and more between Latitude/Longitude points是对地理数据的不同计算的一个很好的参考。打开该页面,然后转到“目标点给定距离并从起点开始承载”部分,有公式,以及在线计算器,因此您可以检查它们(以及您可以在地图上查看点)。公式参数很少:

(lat1,lng1) - your point (marker coordinates)
d - distance (in your case it would be 2.5km)
brng - angle.. 

要找到绑定,您需要找到南,北,东和西矩形边的坐标,因此您将在参数中更改的所有内容都是角度,在您的情况下,它将是0,90,180和270毕业生。公式:

var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
                      Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), 
                             Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));

好吧,指定angle = 0你会发现北,PI / 2 - 东,PI - 南,3 * PI / 2 - 西(角度应该以弧度为单位)。

R = earth’s radius (mean radius = 6,371km)

ADD-ON :刚查看该页面的源代码,因为当我输入在线表格bearing = 0,distance = 2然后我看到地图有两个点并根据地图缩放它们之间的距离真的是2km。那么,你可以在一个简单的attribution许可下使用这个库,没有任何明示或暗示的保证,只需包括它

<script src="http://www.movable-type.co.uk/scripts/latlon.js"></script>

您需要的功能是:

/**
 * Returns the destination point from this point having travelled the given distance (in km) on the
 * given initial bearing (bearing may vary before destination is reached)
 *
 * see http://williams.best.vwh.net/avform.htm#LL
 *
 * @param {Number} brng: Initial bearing in degrees
 * @param {Number} dist: Distance in km
 * @returns {LatLon} Destination point
 */
LatLon.prototype.destinationPoint = function(brng, dist) {
 dist = typeof(dist)=='number' ? dist : typeof(dist)=='string' && dist.trim()!='' ? +dist : NaN;
 dist = dist/this._radius; // convert dist to angular distance in radians
 brng = brng.toRad(); //
 var lat1 = this._lat.toRad(), lon1 = this._lon.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; // normalise to -180...+180

 return new LatLon(lat2.toDeg(), lon2.toDeg());
}

当我输入在线表格bearing = 0,distance = 2时,此方法使用参数latLonCalc.destinationPoint( 0, "2" );

执行

尝试一下,否则给我输入参数,这样我就可以检查出错了什么

UPDATE2 该库适用于Grads,并将它们转换为弧度进行计算,然后再转换为grads。刚刚进行了简单的测试:

var latLonCalc = new  new LatLon( 25, 45 );
var point = latLonCalc.destinationPoint( 0, "2" );
console.info( point );
// prints 25°01′05″N, 045°00′00″E { _lat=25.01798643211838, _lon=45.00000000000005, _radius=6371}

所以入口点和最终目的地之间的距离超过1分钟;

earth = 2 * PI * 6371; // 40 009.98km
=> 40 009.98km / 360grad =~111.14
=> 111,14 / 60 = 1.85 (km/minute) ~2km

这是圆计算,它告诉我最后一点离入口点不远,距离应该是2km;)

答案 1 :(得分:1)

不是你需要的真实的,但请查看这篇文章

http://www.svennerberg.com/2008/11/bounding-box-in-google-maps/

相关问题