2016-08-05 39 views
0

覺得明白了很多,但新項目再次陷入..
我試圖從谷歌地圖API創建的地圖中刪除標記。
我有一個信息窗口內的刪除標記按鈕。的setMap不工作

enter image description here

下面有我的代碼:

function DeleteMarker(index) { 

    console.log(JSON.stringify(markers[index])); 
    markers[index].setMap(null); // problem is here 
    // - setMap is not a function 
    markers[index] = null; 
} 


GMaps.on('click', 
    map.map, 
    function(event) { 

     markers.push(new Point(map.markers.length, event.latLng.lat(), event.latLng.lng())); 

     var index = map.markers.length; 
     var lat = event.latLng.lat(); 
     var lng = event.latLng.lng(); 
     map.addMarker({ 
      lat: lat, 
      lng: lng, 
      title: 'marker' + index, 
      infoWindow: { 
       content: '<p>Details:' 
       + '<p>Latitude:' + event.latLng.lat() + '</p>' 
       + '<p>Longitude:' + event.latLng.lng() + '</p>'+ 
       '<button id ="btnDeleteMarker" onclick=DeleteMarker(\'' + index + '\')>Delete this stop</button>' 
      } 
     }); 
     console.log(JSON.stringify(markers)); 
     map.markers[index].infoWindow.open(map.map, map.markers[index]); 
}); 

有什麼不對?我應該知道什麼才能以正確的方式使用它?

+0

請提供一個[mcve]來演示您的問題。 – geocodezip

+0

看起來像你應該使用'map.markers [index] .setMap(null)'而不是'markers [index] .setMap(null)' – geocodezip

+1

@geocodezip你想知道什麼?我認爲是最小的,完整的和可驗證的例子 – whoiskatrin

回答

1

從我在Google Map API documentation上讀到的內容中,您也必須從markers array中刪除標記。

我會嘗試,是:

  • 清除所有從地圖
  • 標記刪除標記指數
  • ,然後從什麼陣列中的左重新繪製標記。

它應該是這樣的:

function DeleteMarker(index) { 

    console.log(JSON.stringify(markers[index])); 

    setMapOnAll(null);     // Clear all markers from the map 

    var tempArray = markers;   // Create a temporary array 
    unset(tempArray[index]);   // Unset the marker to remove 
    markers = array_values(tempArray); // refresh the markers array 

    setMapOnAll(map);     // Show all markers on the map 

} 

我沒有測試這個...我必須創建一個映射它。
但我會如果上面的代碼失敗。
;)

+0

yeap,我得到了如何解決。非常感謝! – whoiskatrin

+0

不客氣;) –