5

Google ジオコーダ API V3 を使用して、ユーザーが指定した住所に基づいて地図上に位置をプロットしようとしています。コードは以下のとおりです。

直接リクエストを行うと (たとえば、http://maps.googleapis.com/maps/api/geocode/json?address=peterborough&sensor=falseに)、期待どおりの応答が得られます。ただし、以下のコードを使用して同じリクエストを行うと、 getLatLong関数が終了した後、 midpoint変数は常に未定義です。

私は間違って何をしていますか?

function loadFromSearch(address) 
{
  midpoint = getLatLong(address);
  mapCentre = midpoint;
  map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
  ...
}


function getLatLong(address) 
{
  var result;
  var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&sensor=false'
  $.getJSON(url,
  function (data){
     if (data.status == "OK")
     {
        result = data.results[0].geometry.location;
     }
  });
  return result;
}

================================================== ================================

回答を踏まえて、コードを次のように更新しました。Firebug でブレークポイントを設定すると、結果はまだ得られません。結果 = data.results[0].geometry.location; 当たることはありません。

function loadFromSearch(address) 
{
  midpoint = getLatLong(address, loadWithMidpoint);    
}

function getLatLong(address, callback)
{
   var result;
   var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&sensor=false'
   $.getJSON(url,{},
   function (data) {
     if (data.status == "OK")
     {
        result = data.results[0].geometry.location;
        callback(result);
     }
   });
}

function loadWithMidpoint(centre)
{
  mapCentre = centre;
  map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
  ...
}

================================================== ===========================

私はそれを持ってます!動作する最終的なコードは次のようになります。

function loadFromSearch(coordinates, address)
{
  midpoint = getLatLong(address, latLongCallback);
}

function getLatLong(address, callback)
{
  var geocoder = new google.maps.Geocoder();
  var result = "";
  geocoder.geocode({ 'address': address, 'region': 'uk' }, function (results, status) {
     if (status == google.maps.GeocoderStatus.OK)
     {
        result = results[0].geometry.location;
        latLongCallback(result);
     }
     else
     {
        result = "Unable to find address: " + status;
     }
  });
  return result;
}

function latLongCallback(result)
{
  mapCentre = result;
  map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
  ...
}
4

2 に答える 2

10

API の V3 を使用している場合、これは使用できませんか?

function findAddressViaGoogle() {
    var address = $("input[name='property_address']").val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            newPointClicked(results[0].geometry.location)
        } else {
            alert("Unable to find address: " + status);
        }
    });
}

上記は、入力された住所の緯度経度座標を見つけるために使用するものです。

編集:

function loadFromSearch(address) 
{
midpoint = getLatLong(address);
mapCentre = midpoint;
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
...
}


function getLatLong(address) 
{
    var geocoder = new google.maps.Geocoder();
    var result = "";
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            result = results[0].geometry.location;
        } else {
            result = "Unable to find address: " + status;
        }
    });
    return result;
}
于 2010-11-09T10:41:06.187 に答える
4

問題は、$.getJSON関数が非同期であるにもかかわらず、' result' を同期的に返していることです。

このようなことをする必要があります(テストされていません!)

function loadFromSearch(address) 
{
  midpoint = getLatLong(address, function(midpoint){
    // this is a callback
    mapCentre = midpoint;
    map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
    ...           
    });
}

function getLatLong(address, callback) 
{
var result;
var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&sensor=false'
$.getJSON(url,
  function (data) {
    if (data.status == "OK") {
        result = data.results[0].geometry.location;
        callback(result) // need a callback to get the asynchronous request to do something useful 
    }
  });
}

あなたの編集への応答: ああ、V3 ジオコーダーは JSONP をサポートしていないようです。つまり、クロスドメイン リクエストを実行してブラウザからデータを取得することはできません。http://blog.futtta.be/2010/04/09/no-more-jsonp-for-google-geocoding-webservice/を参照してください。

ただし、Brady のソリューションは機能します。それが、Google が私たちにジオコーディングを求めている方法だと思います。

于 2010-11-09T10:44:23.147 に答える