0

私が持っている住所は、results[1].formatted_address530456 などの 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);
      }
    });

私がこれを正しく行っているかどうかはわかりませんし、どこで間違っていたのかもわかりません。この問題の別の方法はありますか? もしそうなら、どのように?

4

1 に答える 1

0

私がこれを正しく行っているかどうかはわかりませんし、どこが間違っていたのかもわかりません。この問題に別の方法はありますか?もしそうなら、どのように?

「正しい」アドレスがわかっている場合は、それを使用してください。アプリケーションの詳細を提供していないため、その方法を提案できません。ジオコーダーから返された座標を住所とともにデータベースに保存することで、ジオコーダーの不確実性を回避できます。エラーが見つかった場合は、修正できます。

ジオコーディング戦略に関するこの記事が役立つ場合があります。

于 2012-07-11T02:37:16.407 に答える