2013-10-03 123 views
1

我目前正在使用Google Maps API v3.13。儘管我的編碼已經停止了,但文檔並沒有真正幫助我。Google Maps API v3:繪製幾何圖形

我所做的是我已經實現了DrawingLibrary,並且我可以在地圖上繪製形狀。當我完成繪製任務時,我想要做的是獲取繪製的形狀的邊框/角(我只激活了折線和矩形)。

然後我想用這個區域來查看它裏面是否有標記,然後讓它們「有彈性」或類似的東西。所以我的問題是,我如何得到用戶畫的區域?這種數據的格式是什麼?每個角落的座標?我是否必須將DrawingLibrary的功能與GeometryLibrary結合起來才能做到這一點?

我已經檢查了這些單證,但仍然沒有能夠找到一個解決方案。 https://developers.google.com/maps/documentation/javascript/geometry https://developers.google.com/maps/documentation/javascript/drawing

這是我到目前爲止有:

function bindOverlayFinishedEvents() { 
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) { 
    if (event.type == google.maps.drawing.OverlayType.POLYGON) { 
     //bounds = event.overlay.getBounds(); 
    } 
    else if (event.type == google.maps.drawing.OverlayType.RECTANGLE) { 
     //bounds = event.overlay.getBounds(); 
    } 
}); 

}

任何幫助將不勝感激!

+1

你跟你貼的代碼有什麼問題? – geocodezip

+1

[google.maps.Polygon](https://developers.google.com/maps/documentation/javascript/reference#Polygon)沒有.getBounds方法,但可以從其路徑計算其邊界。 [google.maps.Rectangle](https://developers.google.com/maps/documentation/javascript/reference#Rectangle)確實有一個getBounds方法,因此_should_可以工作。 – geocodezip

+0

[使用繪圖庫的示例](http://www.geocodezip.com/blitz-gmap-editor/test5.html)如果從編碼中導入多邊形,它會計算其邊界並使用它來居中放大地圖。 – geocodezip

回答

2

因爲你的目標是,以確定標誌是否位於一個區域內,下面的例子演示它取決於形狀類型如何做到:

  • 的圈子:circle.getBounds().contains(latLng) && google.maps.geometry.spherical.computeDistanceBetween(circle.getCenter(), latLng) <= circle.getRadius()
  • 爲矩形:rectangle.getBounds().contains(latLng)
  • 多邊形:google.maps.geometry.poly.containsLocation(latLng,polygon)

包裝函數來確定點是否位於插件IDE的形狀:

//wrapper for contains function 
var shapeContains = function (shape, latLng) { 
    if (shape instanceof google.maps.Circle) 
     return shape.getBounds().contains(latLng) && google.maps.geometry.spherical.computeDistanceBetween(shape.getCenter(), latLng) <= shape.getRadius(); 
    else if (shape instanceof google.maps.Rectangle) 
     return shape.getBounds().contains(latLng); 
    else if(shape instanceof google.maps.Polygon) 
     return google.maps.geometry.poly.containsLocation(latLng, shape); 
    else 
     throw new Error("contains is not supported for this type of shape"); 
} 

Codepen (demo)