3

マーカーを 1 つだけ表示するマップを Google マップで作成しています。ユーザーが初めてマップをクリックすると、マーカーが作成されます (その周りに円が表示されます)。ユーザーがもう一度クリックすると、古いマーカー (および円) が新しい場所に移動します。

基本的に、これはマーカーと円を移動するか、それらを削除して新しいマーカーと円を作成することによって行われます。

現時点では後者の方法を使用しようとしていますが、残念ながらうまくいきません。誰でも助けてもらえますか?

<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>
4

1 に答える 1

4

一度に 1 つのマーカーのみが必要な場合は、マーカーを格納するための配列は必要ありません。setMapメソッドを使用して を渡し、マーカーと円のマップ プロパティをクリアする必要がありますnull。円は間違っています。必要なオプションがすべて揃っているわけではなく、bindTo方法もありません。これがサークルのドキュメントです。

マーカーのプロパティを true に設定しているのを見たので、マーカーにイベントを追加して、サークルのメソッドを使用してマーカーの新しい位置にサークルを再配置するdraggable必要もあります。dragendsetCenter

完全な例を次に示します。

<!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>
于 2012-07-30T13:27:47.080 に答える