2016-08-05 38 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

不客气;) –