【问题标题】:Undefined on Google Map Api v3 Reverse Geocoding [duplicate]在 Google Map Api v3 反向地理编码上未定义 [重复]
【发布时间】:2012-08-13 12:28:07
【问题描述】:

可能重复:
Undefined is not a function, Google Geolocation

我正在尝试获取位置的谷歌反向地理编码,它返回未定义但在 console.log 上显示地址

这是获取值的函数

function getLatLng(latlng, callback) {
    var codedAddress;

    geocoder.geocode({'latLng': new google.maps.LatLng(40.730885,-73.997383)}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            codedAddress = results[1].formatted_address;
            console.log("codedAddress 1 = "+codedAddress);
        } else {
            alert("There was a problem with the map");
        }
        console.log("codedAddress 2 = "+codedAddress);
    });
    callback(codedAddress);
}

这是我的初始化代码

function initialize() {
    var mapOptions = {
        zoom: 11,
        center: new google.maps.LatLng(5.386, 100.245),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

    var location_coords = [
        <?php echo $output; ?>
    ];

    var location_status = [
        <?php echo $status; ?>
    ];

    var location_users = [
        <?php echo $users; ?>
    ];

    var location_date = [
        <?php echo $date; ?>
    ];

    for(i=0;i<location_coords.length;i++) {
      var traffic_options = {
        strokeColor: '#A52A2A',
        strokeOpacity: 0.8,
        strokeWeight: 1,
        fillColor: getColor(location_status[i]),
        fillOpacity: 0.5,
        map: map,
        center: location_coords[i],
        radius: 300
      };
      cityCircle = new google.maps.Circle(traffic_options);

    barr[i] = new Object;

    var marker = new google.maps.Marker({
        position: location_coords[i],
        map: map,
    });

    barr[i].marker = marker;
    barr[i].html = "<div><span style='font-size:9px'>Reported by " + location_users[i] + " on " + location_date[i] + "</span>" +
        "Traffic Location: " + getLatLng(location_coords[i], function(codedAddress) { return codedAddress; }) + 
        "<p>Traffic Condition: " + getCondition(location_status[i]) + "</p></div>";

    barr[i].infoWindow = new google.maps.InfoWindow({
        content: barr[i].html
    });

    barr[i].listener = makeClosure(i, barr[i].marker);
    }
}

问题出在这条语句中

"Traffic Location: " + getLatLng(location_coords[i], function(codedAddress) { return codedAddress; })

我如何在此处获取值而不是“未定义”

谢谢

【问题讨论】:

  • getLatLng() 定义在哪里?

标签: javascript google-maps-api-3


【解决方案1】:

您需要将回调移动到谷歌地图回调函数中:

function getLatLng(latlng, callback) {
  var codedAddress;

  geocoder.geocode({'latLng': new google.maps.LatLng(40.730885,-73.997383)}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        codedAddress = results[1].formatted_address;
        console.log("codedAddress 1 = "+codedAddress);
      } else {
        alert("There was a problem with the map");
      }
      console.log("codedAddress 2 = "+codedAddress);
      callback(codedAddress); //moved here
  });
  //it was here
}

因为您在 Google API 填充变量之前调用了回调。

编辑: 您还需要修改写出位置的代码,因为假设函数调用是同步的。

例如类似:

getLatLng(location_coords[i], function(codedAddress) {
    barr[i].html = "<div><span style='font-size:9px'>Reported by " + location_users[i] + " on " + location_date[i] + "</span>" +
      "Traffic Location: " + codedAddress;  + 
      "<p>Traffic Condition: " + getCondition(location_status[i]) + "</p></div>";
});

【讨论】:

  • 已编辑以包含函数的使用
  • 未捕获的类型错误:无法在 getLatLng 块内设置未定义的属性“html”。你知道为什么吗?
  • 您需要确保bar[i] 变量在范围内(还需要通过使用单独的函数或闭包确保i 是当前值)。您可能想了解异步函数和同步函数之间的区别(您期待后者,但从 Google API 获取前者)
【解决方案2】:

在 getLatLng() 中(doh,就是这样……)您需要将 callback(codedAddress); 移动到地理编码调用中(并使用 latlng 参数):

function getLatLng(latlng, callback) {
  var codedAddress;

  geocoder.geocode({'latLng': latlng}, 
    function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        codedAddress = results[1].formatted_address;
        console.log("codedAddress 1 = "+codedAddress);
      } else {
        alert("There was a problem with the map");
      }
      console.log("codedAddress 2 = "+codedAddress);
    }

    callback(codedAddress);
  );      
}

此函数将返回 void,因此您的回调应初始化 barr 对象。

【讨论】:

    猜你喜欢
    • 2012-06-08
    • 2011-09-27
    • 1970-01-01
    • 2023-03-30
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多