2016-11-12 72 views
-1

我想建立一個多邊形區域作爲圖片使用谷歌地圖,你可以知道如何獲得該地區的座標數組?如何獲得一個點將建立一個多邊形谷歌地圖

enter image description here

picture of desired polygon

+1

相關問題:[OSM to Google Maps polygons](http://stackoverflow.com/questions/36704706/osm-to-google-maps-polygons) – geocodezip

+0

另一個相關問題:http://stackoverflow.com/questions/9706484/add-search-area-outline-to-google-maps-results – xomena

+0

@xomena非常感謝你,你真的幫助我 –

回答

1

可以使用的lat, lng夫婦至少3個元素的陣列構建一個多邊形。

如果陣列沒有關閉,谷歌地圖將默認關閉它。

儘管對於大多數DBMS,您必須保存一個關閉的座標數組。關閉意味着什麼?基本上,數組的第一個和最後一個點是相等的。 DBMS like MongoDB將爲格式不正確的地理空間數據拋出錯誤。 MongoDB Documentation on $polygon。另一件要提及的是,MongoDB僅接受lng, lat格式化的座標。

你可以從Official Google Maps Documentation on Polygons看你能繪製多邊形的座標的數組開始:

// Define the LatLng coordinates for the polygon. 
var triangleCoords = [ 
    {lat: 25.774, lng: -80.190}, 
    {lat: 18.466, lng: -66.118}, 
    {lat: 32.321, lng: -64.757} 
]; 

然後你就可以構建它,並把它在地圖上:

// Construct the polygon. 
var bermudaTriangle = new google.maps.Polygon({ 
    paths: triangleCoords, //Array of Coordinates 
    strokeColor: '#FF0000', //Color of the line 
    strokeOpacity: 0.8, //Color opacity 
    strokeWeight: 3, //Line weight 
    fillColor: '#FF0000', //Inner Color 
    fillOpacity: 0.35 //Inner color opacity 
}); 
//Set it on the map 
bermudaTriangle.setMap(map); 

然後,你可以添加信息到你的多邊形使用clickListenerinfoWindows

// Add a listener for the click event and opens infoWindow 
bermudaTriangle.addListener('click', showArrays); 

infoWindow = new google.maps.InfoWindow; 

你可以閱讀更多關於infoWindows here

最後,here你可以找到一個使用Google Maps Docs和AngularJS製作的Plunker。當然,你可以用純Javascript來做到這一點。

最後但並非最不重要的一點,確保有邊界上的點和合理的座標lat, lng

我希望我一直有幫助。