2011-07-22 39 views
0

我想提交按鈕後移動相同的標記。我有這樣的代碼,它提交按鈕後添加另一點。請幫忙。謝謝。谷歌地圖提交按鈕提交時移動相同的標記

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

@sassi var map後; var marker = null;並將這段代碼放在map.setCenter()後面; – Pratik

+0

是的,它的工作,這是不好的代碼放在這裏。我會附加它的問題。 –