【问题标题】:Google maps geocode API V3 not returning result in javascript functionGoogle 地图地理编码 API V3 未在 javascript 函数中返回结果
【发布时间】:2010-11-09 10:33:24
【问题描述】:

我正在尝试使用 Google geocoder 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 中设置了断点 result = 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);
  ...
}

【问题讨论】:

    标签: javascript jquery google-maps geocoding google-geocoder


    【解决方案1】:

    如果你使用的是 V3 的 API,你不能使用 this 吗?

    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;
    }

    【讨论】:

    • 感谢 Brady,已经尝试过这种方法,但结果仍然永远不会返回除了初始化它的值之外的值。
    • @jibberish - 你肯定在其他地方出了问题,因为如果我运行 getLatLong("York") 它返回 53.9577018,-1.0822855
    • 我确实必须。我可以到达调试器中的 geocoder.geocode 行,然后它跳过函数调用直接返回。我猜我会一直插电!
    • 已解决,请参阅原始问题中的编辑。感谢您在路上帮助我。
    【解决方案2】:

    问题是您的 $.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/

    但是,布雷迪的解决方案确实有效。我想这就是 Google 现在希望我们进行地理编码的方式。

    【讨论】:

    • 谢谢,根据您的建议更新了我的问题,但仍然无法正常工作。
    • 感谢您的帮助,丹。布雷迪的解决方案对我不起作用,将不得不再寻找一些。 +1 成为比我更好的研究人员!
    猜你喜欢
    • 2013-10-05
    • 2012-03-06
    • 2012-04-10
    • 2013-04-05
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多