gpt4 book ai didi

javascript - 谷歌地图获取标记周围的边界框

转载 作者:行者123 更新时间:2023-11-28 21:25:18 25 4
gpt4 key购买 nike

在谷歌地图 API v2 中,我在 map 上有一个标记,我需要计算围绕该标记的边界框。我如何获得一个 5 x 5 公里长且以该标记为中心的接线盒?

最佳答案

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

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

要找到边界,您需要找到南、北、东、西矩形边的坐标,因此您将在参数中更改的所有内容都是 Angular ,在您的情况下,它将是 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));

好吧,指定 Angular = 0,您会发现北、PI/2 - 东、PI - 南、3*PI/2 - 西( Angular 应以弧度传递)。

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

ADD-ON:刚刚查看了该页面的源代码,因为当我在在线表单中输入轴承= 0,距离= 2时,我会看到带有两个点的 map 并根据 map 按比例计算,它们之间的距离实际上是 2 公里。好吧,你可以在简单的 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 该库使用梯度,并将它们转换为弧度进行计算,然后再转换回梯度。刚刚进行了简单的测试:

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

这是一轮计算,它告诉我终点离入口并不远,距离应该是2公里;)

关于javascript - 谷歌地图获取标记周围的边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5219902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com