2013-03-19 68 views
1

我想在這個例子中https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple如何直接在代碼中輸入街道地址

但沒有此輸入字段使用谷歌地圖API V3一樣

<div> 
    <input id="address" type="textbox" value="Sydney, NSW"> 
    <input type="button" value="Geocode" onclick="codeAddress()"> 
</div> 

我想指定的街道地址例如,在碼「75壁ST,紐約」直接。我該怎麼做呢?以下是該示例網址的代碼。任何人都知道需要刪除/添加到此什麼?

<link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet"> 
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
<script> 
    var geocoder; 
    var map; 
    function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var mapOptions = { 
     zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    } 

    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> 
</head> 
<body onload="initialize()"> 
<div> 
<input id="address" type="textbox" value="Sydney, NSW"> 
<input type="button" value="Geocode" onclick="codeAddress()"> 
</div> 
<div id="map-canvas" style="height:90%;top:30px"></div> 

回答

0

我會想象你可以更新功能來做到這一點:

function codeAddress(address) { 
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); 
    } 
}); 

}

而且一旦你要更新的地圖,只要致電:

codeAddress('75 wall st, new york'); 

如果你使用jQuery,你可以把它放在你的文檔就緒函數中,或者在你的代碼的另一部分。

相關問題