如何在使用新地图刷新之前清除谷歌地图标记? [复制]

Posted

技术标签:

【中文标题】如何在使用新地图刷新之前清除谷歌地图标记? [复制]【英文标题】:How to clear the google map markers before refreshing with a new map? [duplicate] 【发布时间】:2019-04-23 23:58:24 【问题描述】:

有人可以分享我如何在用一组新的标记刷新之前清除谷歌地图中的标记吗?

在我的地图中,我从一个包含名称、纬度和经度的数组中添加标记。可以从下拉菜单中选择名称,然后将该名称的所有标记添加到页面中。

项目:Asp.Net Mvc 链接图片:https://i.hizliresim.com/V927Py.jpg

当用户添加标记时,前一组标记保留。我想在添加新集之前删除所有现有标记。

阅读文档后,我尝试了这个:

@
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";

@model List<Project_Map.Models.KONUM>

<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Complex Marker Icons</title>
    <style>
        /* Always set the map height explicitly to define the size of the div
            * element that contains the map. */
        #map 
            height: 100%;
        
        /* Optional: Makes the sample page fill the window. */
        html, body 
            height: 100%;
            margin: 0;
            padding: 0;
        
    </style>
</head>
<body>
    <style>
        #map_wrapper 
            height: 700px;
        

        #map_canvas 
            width: 100%;
            height: 100%;
        
    </style>
    <div id="map_wrapper">
        <div class="mapping" id="map_canvas">
            &nbsp;
        </div>
    </div>

    <div id="map"></div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

    <script>
        jQuery(function ($) 
            var script = document.createElement('script');
            script.src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyBT56XlfxnK2OB4K93vWdrZci_CKjZyQOM&callback=initMap";
            document.body.appendChild(script);
        );
    </script>

    <!-- Google Maps Kodu -->
    <script type="text/javascript">

            var contentString = '<div id="content">' +
                '<div id="siteNotice">' +
                '<img src="#IMG_SRC#" />' +
                '</div>' +
                //'<h2 id="firstHeading" class="firstHeading">#PERSONEL#</h2>' +
                '<div id="bodyContent">' +
                '<b>Mesafe: </b>#MESAFE# Km<br />' +
                '<b>Tarih: </b> #TARIH#' +
                '</p>' +
                '</div>' +
                '</div>';

            $(document).ready(function () 
                initMap();
            );


            function initMap() 

                var mapCenter =  lat: 39.684536, lng: 35.337094 ;


                var map = new google.maps.Map(document.getElementById('map_wrapper'), 
                    zoom: 6,// haritanın yakınlık derecesi
                    center: mapCenter, // haritanın merkezi
                    mapTypeId: google.maps.MapTypeId.HYBRID
                );


                var infoWindow = new google.maps.InfoWindow();

                setInterval(function () 
                   $.ajax(
                      url: '@Url.Action("GetMapLocations", "Konum")',
                      type: "POST",
                      success: function (data) 

                             var json = JSON.parse(data);
                             for (var i = 0; i < json.length; i++) 
                                 var position = 
                                     lat: parseFloat(json[i].lat.replace(',', '.')),
                                     lng: parseFloat(json[i].lng.replace(',', '.'))
                                 ;
                                 var marker = new google.maps.Marker(
                                     position: position,
                                     animation: google.maps.Animation.BOUNCE,
                                     map: map,

                                 );
                                 // infoWindow içeriğini replace et
                                 var cs = contentString;
                                 cs = cs.replace("#PERSONEL#", json[i].name);
                                 cs = cs.replace("#MESAFE#", json[i].mesafe);
                                 cs = cs.replace("#TARIH#", json[i].tarih);
                                 cs = cs.replace("#IMG_SRC#", json[i].img);

                                 google.maps.event.addListener(marker, 'click', (function (marker, cs, infoWindow) 
                                     return function () 
                                         infoWindow.setContent(cs);
                                         infoWindow.open(map, this);
                                         passive: true;
                                     ;
                                 )(marker, cs, infoWindow));
                             ;

                      ,
                           error: function (data)  alert("Malesef Sunucunuza Ulaşamıyoruz. Lütfen Tekrar Deneyiniz..."); ,
                   );
                , 5000);
            ;
    </script>
</body>
</html>

【问题讨论】:

【参考方案1】:

你必须设置一个数组,你可以在其中存储添加的标记

var gmarkers = [];

如果添加标记,则必须将标记对象存储在数组中。

gmarkers.push(marker);

如果你想删除这些标记,你可以使用类似的东西:

function removeMarker() 
if (gmarkers.length > 0) 
    for (var i=0; i<gmarkers.length; i++) 
        if (gmarkers[i] != null) 
            gmarkers[i].setMap(null);
        
    

gmarkers = [];

【讨论】:

感谢您的回答。那么如何编辑这段代码呢? "。那么我该如何编辑这段代码"。我不明白。你的意思是“编辑”我的代码吗?可以直接使用。 是的,请......

以上是关于如何在使用新地图刷新之前清除谷歌地图标记? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 setInterval 刷新时删除地图标记(js)

我们如何在谷歌地图中添加带有点击事件的多个标记? [复制]

如何在谷歌地图中添加两种类型的标记

谷歌地图api清除标记[重复]

谷歌地图 API 标记刷新

如何在添加新标记和图层之前清除所有标记和图层的传单地图?