所以這是我: http://clients.pixelbleed.net/geocoder/geocoder.html從谷歌保存兩個變量映射輸入
我幾乎在我頭頂上這個,但我有第一個輸入框工作 ...這意味着它將採取地址,將其轉換爲緯度/經度並放置在地圖上。我所做的第二個輸入是重複codeAddress()函數並重命名它。
我真的很想做的就是拿兩個輸入框,讓別人在地址上輸入一個地址,將其地址解碼爲經緯度並保存在一個變量中。然後,我可以操縱該變量來完成我最終需要做的等式...只是不知道如何將它們保存爲這樣。有什麼想法嗎?
這是我的JS:
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
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);
}
});
}
function codeAddress2() {
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="" style=" width:800px;height:30px;font-size:10px;">
<input type="button" value="Geocode" onclick="codeAddress()">
</div>
<div>
<input id="address" type="textbox" value="" style=" width:800px;height:30px;font-size:10px;">
<input type="button" value="Geocode" onclick="codeAddress2()">
</div>
<div id="map_canvas" style="height:90%;top:80px"></div>
</body>
您可以創建兩個全局變量,併爲其指定地理位置結果一次回調就是成功。操縱結果,然後你可以在回調函數中調用全局函數,但訣竅是你必須確保在調用全局函數之前分配了兩個變量。 – kjy112 2011-03-28 17:41:05