如何在Google Maps中删除旧的地图标记并更新新的

时间:2019-03-26 07:21:12

标签: javascript google-maps

我正在创建一个获取实时位置并渲染到Google Map的应用程序。在特定的时间间隔后,我必须删除旧的标记并需要渲染标记的标记。

下面是我的代码

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);

我已经使用了Google Map文档中使用的Method。 使用ClearMarkers可以清除旧标记。但就我而言,它不起作用。 当我运行上述代码时,它同时显示旧标记和更新标记。

请参见下面的屏幕截图,其中是重复标记的位置已更新。 enter image description here

3 个答案:

答案 0 :(得分:0)

您将标记初始化为数组:

var marker = []

但是稍后分配一个Marker实例:

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

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

尝试下一步:

  1. 将标记变量重命名为标记
  2. 实现addMarker函数。此功能应将新的标记推入标记数组

答案 1 :(得分:0)

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

gmarkers = []

并将所有新标记放入其中。

gmarkers.push(marker)

要删除标记,我使用了以下功能。

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

答案 2 :(得分:0)

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