2011-11-02 44 views
0

我們使用Google maps API V3,並且我們有一組邊界(當前位於KML文件中)不是方形的(我們以英格蘭的形狀爲例)和當用戶完成一個動作時,我需要從邊界內產生一個隨機的經度和緯度。爲一組邊界內的位置生成lat long

是否有任何聰明的方式做到這一點在JavaScript/jQuery或JavaScript庫,會做到這一點呢?

回答

0

在非矩形形狀來獲得一個隨機點最簡單的方法是:

1) Compute the bounding rectangle (cheap) 
2) Generate a point in the bounding rectangle (cheap) 
3) Check if that point is inside your shape (expensive) 
    3.1) If it is, return the point 
    3.2) If it is not, go back to (2) 

根據形狀的複雜性,這可能足夠。如果已知其他形狀(圓形或其他簡化的形狀),則可以使用其他形狀(如圓形或其他一些簡化的形狀),但OpenLayers確實具有幾何圖元,因此您可以在幾行內實現它。