You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Maps API中place.formatted_address返回undefined问题排查求助

问题原因与解决方法:place.formatted_address 返回 undefined

问题出在你初始化Autocomplete时配置的fields参数——你只指定了["address_components", "geometry", "icon", "name"],而formatted_address不在这个列表里。Google Places API遵循"按需返回"的原则,只会返回你明确声明需要的字段,所以自然拿不到这个值。

只需修改options里的fields数组,把"formatted_address"加进去即可:

const google = window.google;
const options = {
  componentRestrictions: { country: "mo" },
  // 新增 formatted_address 字段
  fields: ["address_components", "geometry", "icon", "name", "formatted_address"],
  strictBounds: false,
  types: ["establishment"],
};
const autocomplete = new google.maps.places.Autocomplete(this.$refs["origin"], options);

google.maps.event.addListener(autocomplete, "place_changed", () => {
  const place = autocomplete.getPlace();
  console.log(place.formatted_address); // 现在能正常拿到值了
  var lat = place.geometry.location.lat();
  var lng = place.geometry.location.lng();
  console.log(lat, lng);
});

简单补充:fields参数的作用是过滤API返回的字段,避免传输不必要的数据,所以务必把所有需要用到的字段都列进去。

内容的提问来源于stack exchange,提问作者DIMSUM LEI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 09:27:25