从谷歌地图xml中删除标记

时间:2015-02-09 15:55:03

标签: javascript google-maps google-maps-api-3 xml-parsing

我正在尝试在Google地图上显示定期刷新的标记。

我发现这段代码会将它们添加到地图中但我需要知道如何在每次刷新时删除它们以便当它们不再在xml中时停止在地图上显示文件。

< script >
var map = null;

function initialize() {
var mapOptions = {
    zoom: 12,
    center: new google.maps.LatLng(53.3478, -6.2597) //center over dublin
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
loadXMLFile();
}

function loadXMLFile() {
var filename = 'data.xml';
$.ajax({
    type: "GET",
    url: filename,
    dataType: "xml",
    success: parseXML,
    error: onXMLLoadFailed
});

function onXMLLoadFailed() {
    alert("An Error has occurred.");
}

function parseXML(xml) {
    var bounds = new google.maps.LatLngBounds();
    $(xml).find("marker").each(function() {
        //Read the name, address, latitude and longitude for each Marker
        var nme = $(this).find('name').text();
        var address = $(this).find('address').text();
        var lat = $(this).find('lat').text();
        var lng = $(this).find('lng').text();
        var markerCoords = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
        bounds.extend(markerCoords);
        var marker = new google.maps.Marker({
            position: markerCoords,
            map: map
        });
    });
    map.fitBounds(bounds);
}
}
google.maps.event.addDomListener(window, 'load', initialize); 
< /script>

当通过调用函数loadXMLFile()添加新标记时,这很有效,但是如果它们被删除则会保留在地图上。我想我需要删除它们https://developers.google.com/maps/documentation/javascript/examples/marker-remove但我不知道如何应用它。

这让我疯了好几天。任何人都可以帮助我吗?

编辑:

我的完整代码未呈现地图:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
#map-canvas {
    height: 400px;
    margin-bottom: 10px;
}      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
    <script>
var map;
var markers = [];

function initialize() {

    var mapOptions = {
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: new google.maps.LatLng(59.76522, 18.35002)
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    google.maps.event.addDomListener(document.getElementById('add-markers'), 'click', addMarkers);
    google.maps.event.addDomListener(document.getElementById('remove-markers'), 'click', removeMarkers);
}

function addMarkers() {

    var coords = [
    new google.maps.LatLng(59.32522, 18.07002),
    new google.maps.LatLng(59.45522, 18.12002),
    new google.maps.LatLng(59.86522, 18.35002),
    new google.maps.LatLng(59.77522, 18.88002),
    new google.maps.LatLng(59.36344, 18.36346),
    new google.maps.LatLng(59.56562, 18.33002)];

    for (var i = 0; i < coords.length; i++) {

        var marker = new google.maps.Marker({
            map: map,
            position: coords[i]
        });

        markers.push(marker);
    }
}

function removeMarkers() {

    for (var i = 0; i < markers.length; i++) {

        markers[i].setMap(null);
    }
}

initialize();

    </script>
  </head>
  <body>
   <div id="map-canvas"></div>
<button id="add-markers">Add markers</button>
<button id="remove-markers">Remove markers</button>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

1)创建一个数组来保存标记

var markers = [];

2)将每个标记添加到地图时将其推送到数组

var marker = new google.maps.Marker({
    position: markerCoords,
    map: map
});

markers.push(marker);

3)当您需要删除它们时,遍历您的数组并在每个标记上调用setMap(null)

for (var i=0; i<markers.length; i++) {

    markers[i].setMap(null);
}

希望这有帮助。

JSFiddle demo

修改

以下是我的修改后更新的代码。我猜你定期调用loadXMLFile()函数(?)。

var map = null;
var markers = [];

function initialize() {
    var mapOptions = {
        zoom: 12,
        center: new google.maps.LatLng(53.3478, -6.2597) //center over dublin
    };
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    loadXMLFile();
}

function loadXMLFile() {

    for (var i = 0; i < markers.length; i++) {

        markers[i].setMap(null);
    }

    var filename = 'data.xml';
    $.ajax({
        type: "GET",
        url: filename,
        dataType: "xml",
        success: parseXML,
        error: onXMLLoadFailed
    });

    function onXMLLoadFailed() {
        alert("An Error has occurred.");
    }

    function parseXML(xml) {
        var bounds = new google.maps.LatLngBounds();
        $(xml).find("marker").each(function () {
            //Read the name, address, latitude and longitude for each Marker
            var nme = $(this).find('name').text();
            var address = $(this).find('address').text();
            var lat = $(this).find('lat').text();
            var lng = $(this).find('lng').text();
            var markerCoords = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
            bounds.extend(markerCoords);
            var marker = new google.maps.Marker({
                position: markerCoords,
                map: map
            });

            markers.push(marker);
        });
        map.fitBounds(bounds);
    }
}
google.maps.event.addDomListener(window, 'load', initialize);