我有一个点 (X,Y),我想创建一个正方形,Google 映射 LatLngBounds 对象,以便使地理编码请求仅偏向这个 LatLngBound 区域。
如何创建这样一个以给定点为中心的 LatLngBounds 正方形?我必须找到NE和SW点。但是在给定距离 d 和点 (x,y) 的情况下如何找到它?
谢谢
我有一个点 (X,Y),我想创建一个正方形,Google 映射 LatLngBounds 对象,以便使地理编码请求仅偏向这个 LatLngBound 区域。
如何创建这样一个以给定点为中心的 LatLngBounds 正方形?我必须找到NE和SW点。但是在给定距离 d 和点 (x,y) 的情况下如何找到它?
谢谢
您也可以getBounds
从定义为圆的半径开始,然后将触发器留给 google。
new google.maps.Circle({center: latLng, radius: radius}).getBounds();
嗯,这很复杂。对于一个粗糙的盒子试试这个:
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 方法。
简单地将 d/2 添加/减去 d/2 到您的 x/y 位置不是吗?
给定 x,y 作为中心点:
NW = x-(d/2),y-(d/2)
SE = x+(d/2),y+(d/2)
不过,请不要相信我——我的数学很糟糕:)
这假设 d 作为“直径”,而不是半径。如果“d”是半径,请不要打扰除以二的部分。