【问题标题】:Why does my javascript geocode function return blank results?为什么我的 javascript 地理编码函数返回空白结果?
【发布时间】:2015-03-24 12:01:33
【问题描述】:

希望你能帮上忙。我做了一个函数,它接收一个 lnglat 点对象并只返回城镇。我可以让它在 console.log 中打印正确的城镇,但它不会从函数返回数据。

我知道这将是一个基本错误,但有人可以查看代码并告诉我。

提前致谢。

function getTownFromPoint(point){

    var geocoder ;
    var geocoder = new google.maps.Geocoder();
    var townset = false;
    mylocation = "";

    geocoder.geocode({latLng: point}, function(results, status) {

        if (status == google.maps.GeocoderStatus.OK) {

            if (results[0]) {

                var components=results[0].address_components;
                for (var component=0;component<(components.length);component++){
                    if(components[component].types[0]=="country" & !townset){
                        mylocation = components[component].long_name;
                    }

                    if(components[component].types[0]=="postal_code" & !townset){
                        mylocation = components[component].long_name;
                    }

                    if(components[component].types[0]=="locality"){
                        mylocation = components[component].long_name;
                        townset = true;
                        console.log(mylocation);
                    }
                }
            }
        }
    });
    return(mylocation);
}

【问题讨论】:

    标签: javascript maps geocode


    【解决方案1】:

    地理编码器是异步的 - 您在设置值之前返回值。

    这里已经回答了:
    Waiting for google maps geocoder?

    【讨论】:

      【解决方案2】:

      那是因为geocode 是一个ajax 调用并且它们是异步的。您需要提供回调函数或使用承诺来获取数据。由于您的问题看起来没有使用jQuery,因此回调可能会更容易:

      以下是您的代码的简化版本,其中包含如何使用回调函数的示例:

      // we pass in the callback as a function parameter
      function getTownFromPoint(point, callback) {
      
        geocoder.geocode({
          latLng: point
        }, function (results, status) {
             var myLocation = results.myLocation;
      
             // then we call the callback with the myLocation variable
             callback(mylocation);
           }
          );
      
      }
      
      // call the function with the point variable and the function
      //  we will use for our callback
      getTownFromPoint(1.223, function (myLocation) {
        console.log(myLocation)
      });
      

      【讨论】:

      • 感谢您的回复。努力让我的头脑稍微了解一下.. 所以我看到你正在使用 console.log 来显示结果.. 如果我想将它传递给函数外部的变量,我该如何把它拿出来?跨度>
      • 因此,您所做的只是传递一个函数,但直到特定时刻才调用它以及 myLocation 变量。当它被调用时,变量作为参数传入,你可以console.log它或其他任何东西。 Here's a longer explanation.
      • @CraigArmitage,基本上你不能很容易,所以你可能必须重组你的代码,让回调变得更容易使用。
      【解决方案3】:

      您面临的问题是您将 geocoder.geocode 函数视为在返回结果之前立即完成。真正发生的是 geocoder.geocode 被触发,然后您会立即返回结果。因为异步结果很可能没有返回,所以你的结果是空的。将地理编码结果视为推动而非拉动。 storeResult 函数(未显示)是保存信息所需的任何代码。因为您将结果与错误字符串组合在一起,所以您必须在 storeResult 函数中处理它。作为替代方案,您可以在结果中显示一个状态,指示成功或失败。

      存储结果:

       storeResult(result);
      

      在你的函数中使用这个函数。这将解决您的问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-23
        • 1970-01-01
        • 2016-06-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多