我有一些幫助,在這裏,我試圖frankenstine兩段代碼在一起,得到以下結果:谷歌地理編碼 - 無法提取座標
用戶點擊地理編碼按鈕 用戶呈現的以下信息(類型地址,城市,國家,州,座標)
這是我用一點幫助拼湊代碼: http://jsfiddle.net/QA7Xr/25/
這是一個完整編寫的代碼:
var myLatlng = new google.maps.LatLng(31.272410, 0.190898);
// INITALIZATION
function initialize() {
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
// GEOCODE
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var address = "",
city = "",
state = "",
zip = "",
country = "";
for (var i = 0; i < results[0].address_components.length; i++) {
var addr = results[0].address_components[i];
if (addr.types[0] == 'country') country = addr.long_name;
else if (addr.types[0] == 'street_address') // address 1
address = address + addr.long_name;
else if (addr.types[0] == 'establishment') address = address + addr.long_name;
else if (addr.types[0] == 'route') address = address + addr.long_name;
else if (addr.types[0] == 'postal_code') zip = addr.short_name;
else if (addr.types[0] == ['administrative_area_level_1']) state = addr.long_name;
else if (addr.types[0] == ['locality']) city = addr.long_name;
}
alert('City: ' + city + '\n' + 'State: ' + state + '\n' + 'Zip: ' + zip + '\n' + 'Country: ' + country);
}
}
});
} else {
alert("Geocode was not successful for the following reason: " + status);
};
});
}
initailize();
document.getElementById("codeAddress").onclick = function() {
codeAddress();
return false;
};
我是JavaScript編程的新手,但它應該工作的方式是,如果地址解析成功,它會抓取變量並將它們轉儲到對話框中,但它只是沒有做到。它在簡單的括號錯誤上寫錯了還是寫了一些更加錯誤的東西?
你的JS又一次搞砸了。太多 };你應該學會正確地縮進你的代碼,它會爲你服務...... – dda 2013-05-11 18:20:22
我在jsfiddle上使用了jstidy,我認爲它會正確縮進:( – Jimmy 2013-05-11 18:21:24