【问题标题】:scope of geocode Google Maps API [duplicate]地理编码 Google Maps API 的范围 [重复]
【发布时间】:2017-03-08 21:48:30
【问题描述】:

console.log 给出了一个未定义的坐标值。如果我们在 if 函数中进行 console.log 坐标,它会给出值,但似乎这些值不能来自 geocode 函数。

有谁知道地理编码的范围是什么以及如何更改以从地理编码函数中获取值?

谢谢

    var coord;

    function change_coord(location) {
    var geocoder = new google.maps.Geocoder();
      geocoder.geocode({'address': location}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
              //real_lat and real_lng are int values 
              var real_lat = results[0].geometry.location.lat();
              var real_lng = results[0].geometry.location.lng();
              coord = {lat: real_lat, lng: real_lng};
          }
      });
  console.log(coord); //undefined
  }

【问题讨论】:

  • 我没有使用过 GeoCoder,但看起来你需要将 console.log(coord) 放在 if 块中,因为 geocoder.geocode 看起来像是对后端的某种异步调用,需要时间来提供结果,在返回之前,您正在打印坐标

标签: javascript google-maps google-maps-api-3 google-maps-api-2 google-geocoder


【解决方案1】:

尝试在地理编码器回调函数中使用 console.log。 在您收到地理编码响应之前,在您的代码中执行 console.log。

【讨论】:

    猜你喜欢
    • 2011-04-23
    • 2013-04-05
    • 1970-01-01
    • 2018-07-02
    • 2010-11-21
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    相关资源
    最近更新 更多