【问题标题】:How to remove old map markers and update new in Google Maps [duplicate]如何删除旧地图标记并在 Google 地图中更新新标记 [重复]
【发布时间】:2026-01-18 07:50:01
【问题描述】:

我正在创建一个应用程序,它可以获取实时位置并呈现给 Google 地图。在特定时间间隔后,我必须删除旧标记并需要渲染更新的标记。

下面是我的代码

function initMap(rtl_data) {
        directionsService = new google.maps.DirectionsService();
        directionsDisplay = new google.maps.DirectionsRenderer();

        var latlng = new google.maps.LatLng(24.238162, 45.156379);
        var myOptions = {
            zoom: 7,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var marker = []
        var map = new google.maps.Map(document.getElementById("map"),myOptions);
        directionsDisplay.setMap(map);

        makeMarkers(rtl_data)

        function makeMarkers(rtl_data){
            // Drivers Location Markers
            deleteMarkers()
            features = []
            marker = []
            drivers_latlng = rtl_data.drivers_latest_location
            drivers_latlng.forEach(function(e){
                features.push({
                    position: new google.maps.LatLng(e.lat, e.lng),
                    type: e.order_id,
                    title: e.driver_id,
                    description: e.order_id ? "Ongoing order: "+e.order_id : "No order assigned."
                })
            })

            image = "/files/truck.png"
            infoWindow = new google.maps.InfoWindow();
            for (i = 0; i < features.length; i++) {
                marker = new google.maps.Marker({
                    position: features[i].position,
                    map: map,
                    title: features[i].title,
                    type: features[i].type,
                    icon: image,
                    description: features[i].description
                });
                //Attach click event to the marker.
                (function (marker) {
                    google.maps.event.addListener(marker, "click", function (e) {
                        //Wrap the content inside an HTML DIV in order to set height and width of InfoWindow.
                        infoWindow.setContent("<div style = 'width:200px;min-height:40px'>" + marker.description + "</div>");
                        infoWindow.open(map, marker);
                    });
                })(marker);
            }

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

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

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

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

        // Fetch Realtime using time interval
        setInterval(function() {
            frappe.call({ // Simple AJAX Call which returns locations and location realated data.
                method: "get_drivers_locations",
                async: false,
                callback: function(r){
                    rtl_data = r.message
                }
            })
            makeMarkers(rtl_data)
        }, 5000);

我已经使用了谷歌地图文档中使用的方法。 使用 ClearMarkers 清除旧标记。但就我而言,它不起作用。 当我运行上面的代码时,它会同时显示旧的和更新的标记。

请参阅下面的屏幕截图,该屏幕截图重复标记更新位置。

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

    您的 init 函数的闭括号是错误的。如果您查看原始文档,则 init 函数在 addmarker 函数之前关闭。在您的示例中,它在 setInterval 之前关闭。所以你的 setInterval 可能甚至无法访问 makeMarker 函数,因为这个函数在另一个函数中。

    【讨论】:

      【解决方案2】:

      阅读以下主题后: Google Map API - Removing Markers 我找到了解决方案。 我创建了一个数组。

      gmarkers = []
      

      并将我所有的新标记都推入其中。

      gmarkers.push(marker)
      

      为了删除标记,我使用了以下函数。

      function setMapOnAll(map) {
          for (var i = 0; i < gmarkers.length; i++) {
              gmarkers[i].setMap(map);
          }
      }
      

      【讨论】:

      • 是的,看来下面的答案是正确的。
      【解决方案3】:

      您将标记初始化为数组:

      var marker = []
      

      但稍后分配一个 Marker 实例:

      marker = new google.maps.Marker({...})
      

      我没有运行您的示例,但我预计该标记变量将不包含标记数组,而是最后创建的标记。

      尝试下一步:

      1. 将标记变量重命名为标记
      2. 实现 addMarker 函数。此函数应将新标记推送到标记数组

      【讨论】:

      最近更新 更多