ここでいくつかの助けがあり、次の結果を得るために2つのコードを一緒にフランケンスティンしようとしました:
ユーザーが [ジオコード] ボタンをクリックします ユーザーに次の情報が表示されます (入力された住所、都市、州、国、座標)
これは、少し助けを借りてつなぎ合わせたコードです: 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プログラミングは初めてですが、ジオコーディングが成功した場合、変数を取得してダイアログボックスにダンプするのがうまくいくはずですが、それはしていません。単純なブラケットエラーで不十分ですか、それともさらに間違った何かが書かれていますか?