Google将地理编码API V3映射为不返回javascript函数的结果

Jas*_*son 5 javascript jquery google-maps geocoding google-geocoder

我正在尝试使用Google地理编码器API V3根据用户指定的地址在地图上绘制位置,代码如下.

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

我做错了什么?

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;
}
Run Code Online (Sandbox Code Playgroud)

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

鉴于回复,我现在已将代码更新为以下内容.我仍然没有得到任何结果,在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);
  ...
}
Run Code Online (Sandbox Code Playgroud)

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

我有它!有效的最终代码如下所示:

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);
  ...
}
Run Code Online (Sandbox Code Playgroud)

Bra*_*ady 9

如果你使用的是API的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);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

以上是我用来找到输入地址的一些长长的坐标,可能效果更好吗?

编辑:

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;
}
Run Code Online (Sandbox Code Playgroud)