javascript - Google maps geocode API V3 未在 javascript 函数中返回结果

标签 javascript jquery google-maps geocoding google-geocoder

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

当我直接发出请求(例如向 http://maps.googleapis.com/maps/api/geocode/json?address=peterborough&sensor=false )时,我得到了预期的响应。但是,当我使用下面的代码发出相同的请求时,midpoint 变量在 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;
}

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

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

最佳答案

如果您使用的是 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;
}

关于javascript - Google maps geocode API V3 未在 javascript 函数中返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4132685/

相关文章:

javascript - 如何在javascript中没有内置函数的情况下查找子字符串?

javascript - 当选择器之一是 'window' 时组合选择器

php - 基于变量 PHP jQuery 显示和隐藏 DIV

javascript - 表单外的语义 UI 提交表单按钮

google-maps - 当谷歌地图超时时,如何使用 RequireJS 捕获加载错误?

javascript - 所有 Google Maps v3 标记的通用事件处理程序

javascript - 同一页面上有两个带标记的 Google map

javascript - 选择 polymer 选项

javascript - 以纯文本形式复制到剪贴板

javascript - 如何在 Google Maps API V3 中调用 fromLatLngToDivPixel?