javascript - 'formatted_address' 对于某些地方为 null,但并非全部(Google map API)这是为什么?

标签 javascript google-maps

我正在使用 Google Maps API for JS,并且 map 上有一堆标记。我有一些代码来显示 map 上每个标记的地址和名称,但地址有时返回为未定义,但有时它确实有效。

function createMarker(place) {
    console.log("adding place " + place.name + " loc=" + place.geometry.location.toUrlValue(6));
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location
    });
    var request = { reference: place.reference };
    service.getDetails(request, function(details, status) {
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent("<b>" + place.name + "</b><br />" + details.formatted_address + "<br /> <a href=" + details.website + ">Visit Website</a>");
        infowindow.open(map, this);
      });
    });
  }

有时,当我单击标记(应该显示信息窗口)时,它会抛出错误而不是显示窗口。错误是

Uncaught TypeError: Cannot read property 'formatted_address' of null

为什么会发生这种情况?如果代码正确,Google 是否有可能不知道所有内容的地址?有人可以帮我解决这个问题吗?

最佳答案

我使用这个 API 已经有大约 4 年了,这很正常。不幸的是,Google Maps API 是一个黑匣子,我们无法修复它,但也许您可以联系他们,以便他们为您提供支持

当他们给你一个回应时,我建议你评估你的变量是否有一个未定义的值,以避免出现错误

关于javascript - 'formatted_address' 对于某些地方为 null,但并非全部(Google map API)这是为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50356805/

相关文章:

javascript - node.js 产生。退出事件立即触发

javascript - swisnl 的 jquery contextMenu 根据单击的元素将自定义数据添加到名称

javascript - sencha/javascript - 如何从 tpl 模板中调用函数

android - 适用于 Android 的重叠标记 Spiderfier

google-maps - Flutter Google Maps插件使所有标记都在视野中

android - 如何在搭载 Android 8 的模拟器上通过 Google Play Services 11 使用 Google Maps Android API

javascript - 禁用单元格的按钮单击事件触发

javascript - 如何在 Javascript 中为特定属性赋值

google-maps - 嵌入式 Google SearchControl map 中的交通图层

python - 通过 Google Cloud Maps Directions API 获取距离