如何在Google地图中添加新圈子之前自动删除以前的圈子

问题描述 投票:-1回答:1

我想先删除第一个圆,然后再添加新的圆,然后单击Google地图的标记。

我正在使用Google Map 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个回答
0
投票

相关问题:

[如果您只想要一个圆,请在创建新圆之前对其进行引用并删除它(如果存在)。添加此:

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

代码段:

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>
© www.soinside.com 2019 - 2024. All rights reserved.