Маркеры Google Карты не очищаются при добавлении новых

1

У меня есть функция, которая берет имя места в качестве входных данных и выдает булавку в положениях lat и lng этого места на карте google. Он также устанавливает границы, используя позиции lat и lng, чтобы установить вывод в окно просмотра. Все работает нормально, но старые маркеры не очищаются при добавлении нового маркера. Я очистил массив маркеров, но он не работает. Вот мой код

  var place = args.address;

        var bounds = new google.maps.LatLngBounds();

        var icon = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
        };

      var geocoder = new google.maps.Geocoder();
        geocoder.geocode({
            'address': place.formatted_address
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results);

                var markers = [];

                 markers.forEach(function(marker) {
                marker.setMap(null);
            });

               markers.push(new google.maps.Marker({
                    map: map.map,
                    icon: icon,
                    title: place.name,
                    position: results[0].geometry.location
                }));


                if (results[0].geometry.viewport) {
                    bounds.union(results[0].geometry.viewport);
                } else {
                    bounds.extend(results[0].geometry.location);
                }

                map.map.fitBounds(bounds);

            } else {
                console.log("error")
            }
        });
Теги:
google-maps
google-maps-api-3

1 ответ

0

Попробуй это

        var place = args.address;

        var bounds = new google.maps.LatLngBounds();

        var markers = []; //

        var icon = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
        };

      var geocoder = new google.maps.Geocoder();
        geocoder.geocode({
            'address': place.formatted_address
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results);                 
                markers.forEach(function(marker) {
                marker.setMap(null);

            });
               markers = []; // Empty here
               markers.push(new google.maps.Marker({
                    map: map.map,
                    icon: icon,
                    title: place.name,
                    position: results[0].geometry.location
                }));


                if (results[0].geometry.viewport) {
                    bounds.union(results[0].geometry.viewport);
                } else {
                    bounds.extend(results[0].geometry.location);
                }

                map.map.fitBounds(bounds);

            } else {
                console.log("error")
            }
        });
javascript google-maps goog

Ещё вопросы

Сообщество Overcoder
Наверх
Меню