2017-04-18 87 views
1

与我的代码,每次你点击地图时出现几个标记。如何删除谷歌地图api标记后设置

我想只是一个单一的标记,每次点击删除上一个标记,并设置新的标记的纬度/ lng到新位置。

这里是我的代码:

var map; 
    var markers = []; 

    function initMap() { 
     var aa = {lat: 32.3896651, lng: 48.3791718}; 

    map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 12, 
     center: aa 
    }); 

    // This event listener will call addMarker() when the map is clicked. 
    map.addListener('click', function(event) { 
     addMarker(event.latLng); 
     document.getElementById("la").value = event.latLng.lat(); 
document.getElementById("lo").value = event.latLng.lng(); 
    }); 



    } 

    // Adds a marker to the map and push to the array. 
    function addMarker(location) { 
    var marker = new google.maps.Marker({ 
     position: location, 
    flat: false, 
    map: map, 
    draggable: true 
    }); 
    markers.push(marker); 
    } 

    // Sets the map on all markers in the array. 
    function setMapOnAll(map) { 
    for (var i = 0; i < markers.length; i++) { 
     markers[i].setMap(map); 
    } 
    } 

    // Removes the markers from the map, but keeps them in the array. 
    function clearMarkers() { 
    setMapOnAll(null); 
    } 

    // Shows any markers currently in the array. 
    function showMarkers() { 
    setMapOnAll(map); 
    } 

    // Deletes all markers in the array by removing references to them. 
    function deleteMarkers() { 
    clearMarkers(); 
    markers = []; 
    } 
+1

看到这个http://stackoverflow.com/questions/20101805/how-to-remove-a-single-marker-from- google-map –

+0

[Google Maps API v3:如何删除所有标记?]的可能重复(http://stackoverflow.com/questions/1544739/google-maps-api-v3-how-to-remove-all-markers ) – 2017-04-18 05:49:31

回答

1

通过增加每一次新的标志物,要创建在你的网页许多对象。 为什么不重新使用现有的标记?做标记全球而不是标记阵列,

var marker = null; 

,然后使用现有的标记对象的setPosition两种改变位置:

marker.setPosition(location); 

通过这种方式,您只使用一个标记,只是在不同的位置。

var map; 
var marker = null; 

function initMap() { 
    var aa = {lat: 32.3896651, lng: 48.3791718}; 

    map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 12, 
     center: aa 
    }); 

// This event listener will call addMarker() when the map is clicked. 
    map.addListener('click', function(event) { 
     addMarker(event.latLng); 
     document.getElementById("la").value = event.latLng.lat(); 
     document.getElementById("lo").value = event.latLng.lng(); 
    }); 

    } 

// Adds a marker to the map and push to the array. 
function addMarker(location) { 
     if(marker) 
      marker.setPosition(location); 
     else 
      marker = new google.maps.Marker({ 
       position: location, 
       flat: false, 
       map: map, 
       draggable: true 
     }); 

    } 

// Sets the map on all markers in the array. 
function setMapOnAll(map) { 

     marker.setMap(map); 

    } 

    // Removes the markers from the map, but keeps them in the array. 
    function clearMarkers() { 
    marker.setMap(null); 
    } 

    // Shows any markers currently in the array. 
    function showMarkers() { 
    marker.setMap(map); 
    } 

    // Deletes all markers in the array by removing references to them. 
    function deleteMarkers() { 
    clearMarkers(); 
    } 
+0

哦,非常感谢,但请更多地解释 –

+0

哦,它是固定的,感谢一位洛神保佑你 –

+0

如果它帮助你,请把它作为答案。 – dev8080