2013-07-26 56 views
1

下面是我的代碼,其中包含表示邊界項的地圖上的邊界和矩形圖的數組。谷歌地圖將矩形合併爲一個多邊形並搜索它

http://jsfiddle.net/XffyE/4/

是否有可能將多個矩形/邊界合併爲一個多邊形?目標是在通過合併矩形創建的多邊形內進行搜索。

例如在合併界限內搜索位置而不是單獨界定每個界限。

function createBounds() { 

     var bounds = new Array(); 

     bounds[0] = new google.maps.LatLngBounds(
      new google.maps.LatLng(25.941886953491675, -80.17411103748543), 
      new google.maps.LatLng(25.947676224813897, -80.16767330177947) 
     ); 
     bounds[1] = new google.maps.LatLngBounds(
      new google.maps.LatLng(25.941886953491675, -80.16767330177947), 
      new google.maps.LatLng(25.94622890698334, -80.1644544339265) 
     ); 
     bounds[2] = new google.maps.LatLngBounds(
      new google.maps.LatLng(25.927413775186118, -80.1644544339265), 
      new google.maps.LatLng(25.94622890698334, -80.15962613214703) 
     ); 
     bounds[3] = new google.maps.LatLngBounds(
      new google.maps.LatLng(25.927413775186118, -80.15962613214703), 
      new google.maps.LatLng(25.931755728677782, -80.15801669822054) 
     ); 
     bounds[4] = new google.maps.LatLngBounds(
      new google.maps.LatLng(25.927413775186118, -80.15801669822054), 
      new google.maps.LatLng(25.933203046508336, -80.15318839644107) 
     ); 
     bounds[5] = new google.maps.LatLngBounds(
      new google.maps.LatLng(25.92886109301667, -80.15318839644107), 
      new google.maps.LatLng(25.933203046508336, -80.15157896251458) 
     ); 

     drawRectangles(bounds); 
    } 

    // Draw the array of bounds as rectangles on the map 
    function drawRectangles(bounds) { 
     boundsRectangles = new Array(bounds.length); 
     for (var i = 0; i < bounds.length; i++) { 
     boundsRectangles[i] = new google.maps.Rectangle({ 
      bounds: bounds[i], 
      fillOpacity: 0, 
      strokeOpacity: 1.0, 
      strokeColor: '#000000', 
      strokeWeight: 1, 
      map: map 
     }); 
     } 
    } 
+0

[Google Maps API檢查標記是否存在於多個邊界中]的可能重複(http://stackoverflow.com/questions/17253929/google-maps-api-check-if-marker-exists-in-multiple-bounds ) –

+0

@ Dr.Molle對於公衆來說,它並不是真的重複,因爲問題是不同的。是的,前一個問題的答案與這個問題有關,但主題涉及標記。這裏我試圖合併邊界而不檢查標記是否存在。 – CyberJunkie

+0

這裏你的問題是如何將多個邊界合併爲1個多邊形,而這正是你在副本中詢問的內容以及已經在那裏回答的問題 –

回答

1

趣味的問題,簡單的答案是:

  1. 找到所有的矩形的頂點
  2. 排序的座標,這些頂點順時針或逆時針(無論谷歌地圖想)
  3. 按排序順序將這些頂點作爲LatLng對象數組輸入到Polygon()構造函數

對不起,我沒有時間詳細瞭解如何找到相反的角落或按角度排序,希望別人可以幫助你,如果這還不夠。

+0

謝謝!我嘗試過,它會創建與單獨的矩形完全相同的單獨多邊形:(困難的部分是僅沿外部矩形選取頂點以創建一個封閉多邊形。我正在查看此http://www.geocodezip.com/v3_polygon_example。 html的例子,但腳本預設所有的座標,而我必須動態創建它們 – CyberJunkie

+0

啊,在你的例子中沒有任何矩形重疊如果他們有你有第三個問題,你需要找到定義在這種情況下,我會以不同的方式來處理這個問題,我會一次添加一個矩形來構建多邊形,並在每個步驟中拋出現有多邊形內的頂點,假設您有某種方法檢查一個點是否在多邊形中,這裏的一些代碼在這裏:https://github.com/tparkin/Google-Maps-Point-in-Polygon。我會更新我的答案來反映這一點。 – nwellcome