谷歌地图 API V3 - 只含有一个标记的地图

3
我正在Google Maps中创建一张地图,我希望只有一个标记。当用户第一次点击地图时,会创建一个标记(周围有一个圆圈)。当用户再次点击时,旧的标记(和圆圈)将被移动到新位置。
因此,这可以通过移动标记和圆圈或删除它们并创建新的标记和圆圈来完成。
目前我正在尝试使用后者的方法,但不幸的是它没有起作用。有人能帮忙吗?
<script>
  //Initial variables (map and the markers array, which we're going to try to get rid of)
  var map;
  var markersArray = [];

  //and GO!
  function initialize()
  {
    //need to correct our starting location - over UK would be nice
    var haightAshbury = new google.maps.LatLng(37.7699298, -122.4469157);
    var mapOptions =
    {
      zoom: 12,
      center: haightAshbury,
      mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
    //listen out for clicks and, when clicked, add a marker
    google.maps.event.addListener(map, 'click', function(event)
    {
      addMarker(event.latLng);
    });
  }

  // Add a marker to the map and push to the array.
  function addMarker(location)
  {
    markersArray = [];
    setAllMap(null);
    var marker = new google.maps.Marker(
    {
      position: location,
      map: map,
      draggable: true
    });
    //create a circle
    var circle = new google.maps.Circle(
    {
      map: map,
      radius: 3000
    });
    //bind the circle to the marker we've also just created
    circle.bindTo('center', marker, 'position');
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

2
一个完整的示例会更容易帮助理解。setAllMap是什么?在清空数组后,您可能不想调用它。如果您只想要一个标记,为什么还需要markersArrays?最简单的方法是只有一个“标记”变量,并将其移动(或删除并重新创建)。 - geocodezip
1个回答

4
如果您只想一次显示一个标记,您不需要使用数组来存储标记。您必须确保使用setMap方法并传递null来清除标记和圆的地图属性。圆形是错误的,您没有所有需要的选项,也没有bindTo方法。这里是圆形文档
我看到您将标记的draggable属性设置为true,因此您还需要在标记上添加一个dragend事件,使用圆上的setCenter方法重新定位圆到标记的新位置。
这是一个完整的示例:
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Google Maps</title>
    <style>
    #map_canvas{
        height:800px;
        width:800px;
    }   
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false">
    </script>
    <script>        
      var marker, myCircle, map;
      function initialize() {
        var myLatlng = new google.maps.LatLng(37.7699298, -122.4469157);
        var mapOptions = {
          zoom: 12,
          center: myLatlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById('map_canvas'), 
            mapOptions);

        google.maps.event.addListener(map, 'click', function(event){
            addMarker(event.latLng);
        });
      }

      function addMarker(latLng){       
        //clear the previous marker and circle.
        if(marker != null){
            marker.setMap(null);
            myCircle.setMap(null);
        }

        marker = new google.maps.Marker({
            position: latLng,
            map: map,
            draggable:true
        });

        //circle options.
        var circleOptions = {
            strokeColor: '#FF0000',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: '#FF0000',
            fillOpacity: 0.35,
            map: map,
            center: latLng,
            radius: 3000
          };
         //create circle
        myCircle = new google.maps.Circle(circleOptions);

        //when marker has completed the drag event 
        //recenter the circle on the marker.
        google.maps.event.addListener(marker, 'dragend', function(){
            myCircle.setCenter(this.position);
        });     
    }
    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas"></div>
  </body>
</html>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接