我在这里得到了一些帮助,我试图将两段代码合并在一起以获得以下结果:
用户单击地理编码按钮 用户会看到以下信息(键入的地址、城市、州、国家、坐标)
这是我在一些帮助下拼凑的代码: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 编程的新手,但它应该工作的方式是,如果地理编码成功,它会抓取变量并将它们转储到对话框中,但它只是没有这样做。它是在一个简单的括号错误上不足还是写了一些更错误的东西?