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

控制台返回错误JSON对象结果,需提取完整地址经纬度值

从place对象提取完整地址、纬度和经度的解决方案

嘿,我来帮你搞定这个提取地址和经纬度的问题!根据你给出的place JSON结构(看起来是Google Places API的返回格式),我给你两种靠谱的实现方式:

一、提取纬度(lat)和经度(long)

首先,标准的Places API返回里,经纬度会放在geometry.location对象下,注意通常字段名是lng而不是long,不过如果你的数据确实是long,直接替换就行。代码示例:

// 提取纬度
const latitude = place.geometry.location.lat;
// 提取经度(如果是API返回的标准格式,这里应该是lng,根据你的实际字段调整)
const longitude = place.geometry.location.lng; // 或者 place.geometry.location.long;

注:如果是Google Maps JavaScript API的google.maps.LatLng对象,lat和lng是方法,需要加括号调用:place.geometry.location.lat()

二、提取完整地址

这里有两种方式,优先用第一种,更省心:

方式1:直接用现成的格式化地址

如果你的place对象包含formatted_address字段(这是API自动拼接好的标准地址,适配不同地区格式),直接拿过来用就行:

const fullAddress = place.formatted_address || '地址未找到';

方式2:手动拼接address_components

如果没有formatted_address字段,就需要从address_components里提取对应部分再拼接。我们可以先按地址类型分类提取,再组合起来:

// 先初始化各地址部分
const addressParts = {
  streetNumber: '',
  route: '',
  city: '',
  region: '',
  country: ''
};

// 遍历address_components,匹配对应的类型
place.address_components.forEach(component => {
  if (component.types.includes('street_number')) {
    addressParts.streetNumber = component.long_name;
  } else if (component.types.includes('route')) {
    addressParts.route = component.long_name;
  } else if (component.types.includes('locality')) {
    addressParts.city = component.long_name;
  } else if (component.types.includes('administrative_area_level_1')) {
    addressParts.region = component.long_name;
  } else if (component.types.includes('country')) {
    addressParts.country = component.long_name;
  }
});

// 过滤空值后拼接成完整地址,分隔符和顺序可以根据需求调整
const fullManualAddress = [
  addressParts.streetNumber,
  addressParts.route,
  addressParts.city,
  addressParts.region,
  addressParts.country
].filter(part => part).join(', ');

这样你就能得到符合需求的完整地址和经纬度啦!

内容的提问来源于stack exchange,提问作者rob.m

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:17:43