问题描述
|
我有一个点(X,Y),我想创建一个square,Google映射LatLngBounds对象,以便使地理编码请求仅偏向该LatLngBound区域。
如何创建以给定点为中心的LatLngBounds正方形?我必须找到NE和SW点。但是,如何在给定距离d和点(x,y)的情况下找到它呢?
谢谢
解决方法
您也可以从定义为圆的半径开始向“ 0”倾斜,然后将触发保留给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,225,10);
编辑
以上是他们在2011年撰写本文时所做的方法。这些天,Google Maps api的使用方法非常宽松。 @wprater的答案更加整洁,并使用了一些较新的api方法。,简单地将d / 2加/减到您的x / y位置上不行吗?
以x,y为中心:
NW = x-(d/2),y-(d/2)
SE = x+(d/2),y+(d/2)
但是,请不要相信我-我在数学方面很糟糕:)
假设d为\“直径\”,而不是半径。如果\“ d \”是半径,请不要担心被二分频的问题。