【问题标题】:How to remove previous circle automatically before adding new circle in Google map如何在谷歌地图中添加新圈子之前自动删除以前的圈子
【发布时间】:2020-04-15 23:01:01
【问题描述】:

我想在添加新圆圈之前删除第一个圆圈点击谷歌地图的标记。

我正在使用谷歌地图 API: https://developers.google.com/maps/solutions/store-locator/clothing-store-locator

网站链接:http://premium-gates.com/bft/dealers/

搜索测试文本:5427DG

这是在标记上添加圆圈的代码。

function createMarker(latlng, name, address) {
    var image = '<?php echo get_template_directory_uri() ?>/assets/images/map-pin.png';

    var html = "<div class='map-popup'><b>" + name + "</b> <p>" + address + "</p></div>";
    var marker = new google.maps.Marker({
        map: map,
        position: latlng,
        icon: image
    });

    google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
    });

    marker.addListener('click', function(event) {
        addCircle(event.latLng);
    });

    markers.push(marker);
}

function addCircle(location) {
    cityCircle = new google.maps.Circle({
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 0.2,
        fillColor: '#FF0000',
        fillOpacity: 0.2,
        map: map,
        center: location,
        radius: 1000,
        draggable:false
    });
}

当我点击标记时,如何在添加新圆圈之前删除圆圈?

【问题讨论】:

    标签: javascript php google-maps google-maps-api-3 google-maps-markers


    【解决方案1】:

    相关问题:

    如果您只想要一个圆,请保留对它的引用并在创建新圆之前将其删除(如果存在)。添加这个:

    if (cityCircle && cityCircle.setMap) 
      cityCircle.setMap(null);
    

    addCircle函数:

    var cityCircle;
    function addCircle(location) {
      if (cityCircle && cityCircle.setMap)
        cityCircle.setMap(null);
      cityCircle = new google.maps.Circle({
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 0.2,
        fillColor: '#FF0000',
        fillOpacity: 0.2,
        map: map,
        center: location,
        radius: 1000,
        draggable: false
      });
    }
    

    proof of concept fiddle

    代码 sn-p:

    var markers = [];
    var map;
    var infoWindow;
    
    function initMap() {
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: {
          lat: -33.9,
          lng: 151.2
        }
      });
      infoWindow = new google.maps.InfoWindow();
      setMarkers(map);
    }
    
    var beaches = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];
    
    function setMarkers(map) {
      for (var i = 0; i < beaches.length; i++) {
        var beach = beaches[i];
        createMarker({
          lat: beach[1],
          lng: beach[2]
        }, beach[0], beach[0]);
      }
    }
    
    function createMarker(latlng, name, address) {
    
      var html = "<div class='map-popup'><b>" + name + "</b> <p>" + address + "</p></div>";
      var marker = new google.maps.Marker({
        map: map,
        position: latlng,
      });
    
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    
      marker.addListener('click', function(event) {
        addCircle(event.latLng);
      });
    
      markers.push(marker);
    }
    
    var cityCircle;
    
    function addCircle(location) {
      if (cityCircle && cityCircle.setMap)
        cityCircle.setMap(null);
      cityCircle = new google.maps.Circle({
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 0.2,
        fillColor: '#FF0000',
        fillOpacity: 0.2,
        map: map,
        center: location,
        radius: 1000,
        draggable: false
      });
    }
    /* 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;
    }
    <div id="map"></div>
    <!-- Replace the value of the key parameter with your own API key. -->
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
    </script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 2011-10-09
      • 2012-12-05
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多