【问题标题】:Return value with google maps geocoder [duplicate]使用谷歌地图地理编码器返回值[重复]
【发布时间】:2013-07-28 10:42:15
【问题描述】:

我只是找不到这段代码有什么问题:

function getLocationName(latitude, longitude) {
    if (isNaN(parseFloat(latitude)) || isNaN(parseFloat(longitude))) {
        return false;
    }

    var locationName;
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(latitude, longitude)

    // Reverse Geocoding using google maps api.
    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                locationName = results[1].formatted_address;
                alert(locationName);
            }
            else {
                locationName = "Unknown";
            }
        }
        else {
            locationName = "Couldn't find location. Error code: " + status;
        }
    });
    alert(locationName);
    return locationName;

}

我从这样的 jquery 事件处理程序中调用它:

$("#id").on("event", function (event, ui) {
    $("#userLocation").text(getLocationName(latitude, longitude));
});

奇怪的是,第一个警报得到了正确的“locationName”值,但第二个总是返回“未定义”。我尝试用一​​个值初始化变量,在这种情况下,第一个警报再次返回正确的位置名称,但第二个警报返回了初始化值。这给了我一个概念,这可能是一个与变量范围相关的问题,但我不知道是什么。

附言。我没有任何其他同名变量(本地/全局)。

更新:警报现在可以正常工作(感谢 Lwyrn 的回答),但返回值仍然错误。我已经按照链接的 SO 问题中的答案进行了操作,但仍然无法“返回”正确的值。警报确实工作正常。

【问题讨论】:

  • 这不是由于变量范围,而是由于代码的异步特性。 stackoverflow.com/questions/2993563/…
  • 那你没有仔细阅读链接的问题。 没有办法从异步操作中返回一个值,您需要使用回调。 (好的,如果您自己进行 AJAX 调用,您可以使其同步而不是异步,但由于很多原因,这是错误的。)

标签: javascript jquery google-maps google-maps-api-3


【解决方案1】:

你必须移动“alert(locationName);”进入 geocoder.geocode 回调。因为 geocoder.geocode 执行一个 AJAX 请求。当您抛出警报时,var locationName 仍然未定义(未设置)。 试试这样

function getLocationName(latitude, longitude, callback) {
    if (isNaN(parseFloat(latitude)) || isNaN(parseFloat(longitude))) {
        return false;
    }

    var locationName;
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(latitude, longitude)

    // Reverse Geocoding using google maps api.
    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                locationName = results[1].formatted_address;
                alert(locationName);
            }
            else {
                locationName = "Unknown";
            }
        }
        else {
            locationName = "Couldn't find location. Error code: " + status;
        }
        alert(locationName);
        callback(locationName);
    });
}

要获得“回报”,您必须创建自己的回调。 像这样试试

$("#id").on("event", function (event, ui) {
    getLocationName(latitude, longitude, function(result){
        $("#userLocation").text(result);
    });
});

关于alert,在ajax请求之前调用return。所以当ajax请求完成他的工作时,你必须使用回调来调用!

【讨论】:

  • 嗨。感谢您的回答。这类作品。警报“提醒”正确的值,但返回不会将正确的值返回给我从中调用 getLocationName() 的函数。有什么想法吗?
  • 我已经编辑了回复!
  • 谢谢。效果很好。
  • 如何将结果分配给变量?我试过 var x = getLocationName(latitude, longitude, function(result){ return result; });警报(x);但这会提醒未定义。知道为什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多