我有一些帮助,在这里,我试图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