2016-06-28 78 views
0

如何刪除裝載了loadGeoJson的谷歌地圖的數據圖層上的頂點(多邊形點)?刪除Google地圖DataLayer頂點

http://output.jsbin.com/tuyived

我想刪除右鍵單擊白色圓圈

+0

其實我想要實現這個https://developers.google.com/maps/documentation/javascript/examples/delete-vertex-menu在數據層https://開頭developers.google.com/maps/documentation/javascript/datalayer#overview –

回答

4

你可以用下面的代碼片段刪除所有(例如,將L從谷歌的左上角)一個頂點:

map.data.forEach(function(feature) { 
    //If you want, check here for some constraints. 
    map.data.remove(feature); 
}); 

參考:Remove all features from data layer

編輯:

如果要刪除剛剛點擊的頂點是比較麻煩一些,但我可以通過以下點擊處理程序來完成:

map.data.addListener('click', function(ev) { 

    //array to hold all LatLng in the new polygon, except the clicked one 
    var newPolyPoints = []; 

    //iterating over LatLng in features geometry 
    ev.feature.getGeometry().forEachLatLng(function(latlng) { 

     //excluding the clicked one 
     if (latlng.lat() == ev.latLng.lat() && latlng.lng() == ev.latLng.lng()) { 
      console.log('This one will be removed: lat: ' + latlng.lat() + ', lng: ' + latlng.lng()); 
     } else { 
      //keeping not matching LatLng 
      newPolyPoints.push(latlng); 
     } 
    }); 

    //creating new linear ring 
    var newLinearRing = new google.maps.Data.LinearRing(newPolyPoints); 

    //creating a new polygon out of the new linear ring 
    var newPoly = new google.maps.Data.Polygon([newLinearRing]); 

    //apply the new polygon to the clicked feature 
    ev.feature.setGeometry(newPoly); 
}); 

您可能需要根據自己的需要來調整它/你的數據結構體。它適用於所提供的結構。

希望這一次幫助;)

+0

也許我的問題不完整。請檢查更新後的問題。 –

+0

編輯答案匹配更新的問題 –

+0

很好的答案。謝謝! –