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();
那很复杂.对于一个粗糙的盒子试试这个:
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方法.