私が持っている住所は、results[1].formatted_address
530456 などの 6 つの数字を含むデータベースから取得した連結です。次に、それをジオコーディングし、Google マップにマーカーを配置しました。この部分は成功です。
マーカーの情報ウィンドウに完全な住所を表示したいresults[0].formatted_address
ので、マーカーの latLng を逆ジオコーディングして完全な住所を取得しましたが、マップ全体が消えてしまいました。私がやった方法は次のとおりです。
var address;
var eventwindow;
function codeAddress(postal) {
geocoder.geocode( {'address': postal + ", Singapore"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
eventwindow = new google.maps.InfoWindow();
var markerE = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
});
address = getMarkerAddress(results[0].geometry.location);
google.maps.event.addListener(marker, 'click', function(){
if(eventwindow)
eventwindow.close();
eventwindow = new google.maps.InfoWindow({
content: address,
});
eventwindow.open(map,marker);
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function getMarkerAddress(location) {
geocoder.geocode({'latLng': location}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if(results[0])
{
address = results[1].formatted_address.substring(10);
}
}
else {
alert("Geocoder failed due to: " + status);
}
});
私がこれを正しく行っているかどうかはわかりませんし、どこで間違っていたのかもわかりません。この問題の別の方法はありますか? もしそうなら、どのように?