2010-06-29 61 views
3

我有一個緯度/經度值和距離值。我需要計算一個以給定位置爲中心的邊界框。所以如果距離是200米,那麼矩形框應該在前面,後面,左邊和右邊200米。使用Javascript計算邊界框

我該如何去做這個使用JavaScript?

回答

3

您需要將座標lat/long翻譯爲您正在使用的地圖投影中的x/y值,然後才能計算出您的邊界框。

我不太瞭解Google Maps API,而且我也不確切知道你想要用你的盒子做什麼。但也許GBounds,GMercatorProjectionGLatLngBounds可能會有所幫助。如果Google Maps API不支持您正在使用的地圖投影的計算,那麼使用Proj4js會很有幫助。也許你想讀一下Map projections。谷歌地圖默認使用Mercator projection

3

這裏有許多有用的JavaScript函數與緯度和經度的工作:

http://www.movable-type.co.uk/scripts/latlong.html

對於點周圍的邊框,簡單使用the above javascript library修改可能是:

LatLon.prototype.boundingBox = function (distance) 
{ 
    return [ 
     this.destinationPoint(-90, distance)._lon, 
     this.destinationPoint(180, distance)._lat, 
     this.destinationPoint(90, distance)._lon, 
     this.destinationPoint(0, distance)._lat, 
    ]; 
} 

(這使用"Destination point given distance and bearing from start point"計算。)