2011-07-22 3 views
0

ボタンを送信した後に同じマーカーを移動したいと思います。私はこのようなコードを持っている、それはボタンを送信した後に別のポイントを追加する。助けてください。ありがとう。ここボタンを送信するとgoogle mapsが同じマーカーを移動します

http://project-amma.info/map6.html

<script type="text/javascript"> 
    var geocoder; 
    var map; 

    function initialize() { 
     geocoder = new google.maps.Geocoder(); 
     var latlng = new google.maps.LatLng(6.5,80.0); 
     var myOptions = { 
     zoom: 12, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    } 

    function codeAddress() { 
     var address = document.getElementById("address").value; 
     geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location); 
      var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
      }); 
     } else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
     }); 
    } 
</script> 

<body onload="initialize()"> 
<div> 
<input id="address" type="textbox" value="" /> 
<input type="button" value="Geocode" onclick="codeAddress()" /> 
</div> 
<div id="map_canvas" style="height:90%"> 
</div> 
</body> 

正解です...

var marker = null; 

function codeAddress() { 
     var address = document.getElementById("address").value; 
     geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location); 

      if(marker == null){ 
       marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
       }); 
      }else 
       marker.setPosition(results[0].geometry.location); 
     } else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
     }); 
    } 

答えて

1

はあなたが意味する、返信用

if(marker == null){ 
    marker = new google.maps.Marker({ 
    map: map, 
    position: results[0].geometry.location 
    }); 
}else 
    marker.setPosition(results[0].geometry.location); 
+0

感謝を宣言マップした後、この 宣言マーカーを試してみてくださいその後は? –

+0

var map後の@sassi; varマーカー= null; map.setCenter()の後にこのコードを入れてください。 – Pratik

+0

はい、動作しています。ここにコードを入れるのはうれしいことではありません。私はそれを質問に追加します。 –

関連する問題