谷歌映射了在javascript函数中不返回结果的地理代码API V3

时间:2021-07-31 22:59:26

I'm trying to use the Google geocoder API V3 to plot a location on a map based on an address specified by the user, code is below.

我正在尝试使用谷歌geocoder API V3根据用户指定的地址在地图上绘制位置,代码如下。

When I make a request directly (e.g. to http://maps.googleapis.com/maps/api/geocode/json?address=peterborough&sensor=false) I get the expected response. However, when I make the same request using the code below, the midpoint variable is always undefined after the getLatLong function has exited.

当我直接提出一个请求(例如:http://maps.googleapis.com/maps/api/geocode/json?address=peterborough&sensor=false)时,我将得到预期的响应。但是,当我使用下面的代码发出相同的请求时,在getLatLong函数退出之后,中间点变量总是未定义。

What am I doing incorrectly?

我做错了什么?

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

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

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

In light of responses, I have now updated the code to the following. I'm still not getting any result though, with breakpoints set in Firebug the result = data.results[0].geometry.location; never gets hit.

根据响应,我现在将代码更新为以下代码。但是,我仍然没有得到任何结果,因为在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);
  ...
}

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

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

I have it! The final code, which works, looks like this:

我有它!最终的代码是这样的:

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

2 个解决方案

#1


9  

If you are using V3 of the API cannot you use the this?

如果你用的是V3 API你不能用这个吗?

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

The above is what I use to find some lat long cordinates of an inputted address, May work better?

上面是我用来找一些很长很密的输入地址,可以更好的工作吗?

EDIT:

编辑:

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

#2


4  

The problem is your $.getJSON function is asynchronous, yet you are returning the 'result' synchronously.

问题出在你身上。getJSON函数是异步的,但是您同时返回“结果”。

You need to do something like this (not tested!)

您需要做这样的事情(没有测试!)

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

In response to your edit: Oh dear, it looks like the V3 geocoder does not support JSONP. This means you can not do a cross-domain request to get data from it in your browser. See http://blog.futtta.be/2010/04/09/no-more-jsonp-for-google-geocoding-webservice/

对于您的编辑:噢,亲爱的,看起来V3 geocoder不支持JSONP。这意味着您不能通过跨域请求从浏览器中获取数据。参见http://blog.futtta.be/2010/04/09/no-more-jsonp-for-google-geocoding-webservice/

However Brady's solution does work. I guess that is the way Google want us to geocode now.

然而,布雷迪的方案确实有效。我猜这就是谷歌现在希望我们做地理编码的方式。

#1


9  

If you are using V3 of the API cannot you use the this?

如果你用的是V3 API你不能用这个吗?

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

The above is what I use to find some lat long cordinates of an inputted address, May work better?

上面是我用来找一些很长很密的输入地址,可以更好的工作吗?

EDIT:

编辑:

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

#2


4  

The problem is your $.getJSON function is asynchronous, yet you are returning the 'result' synchronously.

问题出在你身上。getJSON函数是异步的,但是您同时返回“结果”。

You need to do something like this (not tested!)

您需要做这样的事情(没有测试!)

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

In response to your edit: Oh dear, it looks like the V3 geocoder does not support JSONP. This means you can not do a cross-domain request to get data from it in your browser. See http://blog.futtta.be/2010/04/09/no-more-jsonp-for-google-geocoding-webservice/

对于您的编辑:噢,亲爱的,看起来V3 geocoder不支持JSONP。这意味着您不能通过跨域请求从浏览器中获取数据。参见http://blog.futtta.be/2010/04/09/no-more-jsonp-for-google-geocoding-webservice/

However Brady's solution does work. I guess that is the way Google want us to geocode now.

然而,布雷迪的方案确实有效。我猜这就是谷歌现在希望我们做地理编码的方式。