Удаление почтовых маркеров Google с помощью jquery после получения их из xml - PullRequest
0 голосов
/ 23 мая 2010

Я пытаюсь создать страницу, которая содержит карту Google. Карта заполнена маркерами из XML-файла. Я просто не могу понять, как удалить «старые» маркеры, которые не соответствуют последним данным, введенным пользователем. В данный момент мой js останавливается после самого первого элемента xml.

clearList.push (маркер); Предполагается убрать сгенерированный маркер для дальнейшего использования. Когда пользователь нажимает кнопку поиска, я хочу, чтобы все маркеры исчезли, и использую clearMarkers ();.

Может быть, кто-то здесь может помочь Себастьян

Вот мой JavaScript:

$(document).ready(function() {
  $("#map").css({height: 650});
    var clearList = [];
    var myLatLng = new google.maps.LatLng(52.518143, 13.372879);
    MYMAP.init('#map', myLatLng, 11);

  $("#showmarkers").click(function(e){
            clearMarkers();
        MYMAP.placeMarkers('markers.xml');

  });

});

function clearMarkers() {
    $(clearList).each(function () {
         this.setmap(null);
    });
    clearList = [];
}



var MYMAP = {
    map: null,
    bounds: null
    }

MYMAP.init = function(selector, latLng, zoom) {
    var myOptions = {
    zoom:zoom,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.HYBRID
    }
    this.map    =   new google.maps.Map($(selector)[0], myOptions);
    this.bounds =   new google.maps.LatLngBounds();
    }

MYMAP.placeMarkers = function(filename) {
    $.get(filename, function(xml){  
        $(xml).find("marker").each(function(){
            // read values from xml for searching

            var platzart =  $(this).find('platzart').text();
            var ort      =  $(this).find('ort').text();
            var open     =  $(this).find('open').text();

            if (platzart    =="Kunstrasen"  && $('#kunstrasen').attr('checked')
                || platzart =="Rasen"       && $('#rasen').attr('checked') 
                || platzart =="Tartan"      && $('#tartan').attr('checked')
                || platzart =="Boltzplatz"  && $('#boltzplatz').attr('checked')
                ){

            // read values from xml for additional info
            var name    =   $(this).find('name').text();
            var plz     =   $(this).find('plz').text();
            var note    =   $(this).find('note').text();
            var adress  =   $(this).find('adress').text();

            // create a new LatLng point for the marker
            var lat     =   $(this).find('lat').text();
            var lng     =   $(this).find('lng').text();
            var point   =   new google.maps.LatLng(parseFloat(lat),parseFloat(lng));

            // extend the bounds to include the new point
            MYMAP.bounds.extend(point);

            // create new marker
            var marker = new google.maps.Marker({
                position: point,
                map: MYMAP.map
            });
            clearList.push(marker);

            // add onclick overlay
            var infoWindow = new google.maps.InfoWindow();
            var html='<strong>'+name+'</strong.><br />'+platzart;
            google.maps.event.addListener(marker, 'click', function() {
                infoWindow.setContent(html);
                infoWindow.open(MYMAP.map, marker);
            });
            }
            MYMAP.map.fitBounds(MYMAP.bounds);
        });
    });
}

Заранее спасибо

1 Ответ

0 голосов
/ 24 мая 2010

Похоже, у вас есть опечатка:

function clearMarkers() {
    $(clearList).each(function () {
         this.setmap(null);
    });
    clearList = [];
}

должно быть:

function clearMarkers() {
    $(clearList).each(function () {
         this.setMap(null);
    });
    clearList = [];
}

this.setmap должно быть this.setMap.

...