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

如何从嵌入网站的Google地图中获取标记地点的经纬度?

获取Google地图标记地点的纬度和经度

你当前的代码已经包含了获取经纬度所需的核心数据——因为你在Autocomplete的fields参数中指定了"geometry",同时Geocoder返回的结果也包含了位置信息。以下是两种获取并使用经纬度的具体方式:

方式一:直接从Autocomplete返回的place对象提取

用户选择地点后,autocomplete.getPlace()返回的place对象已包含geometry.location属性,直接调用lat()和lng()方法就能拿到经纬度:

autocomplete.addListener("place_changed", () => {
  infowindow.close();
  const place = autocomplete.getPlace();

  if (!place.place_id || !place.geometry) {
    return;
  }

  // 提取经纬度
  const latitude = place.geometry.location.lat();
  const longitude = place.geometry.location.lng();

  // 可根据需求使用:比如打印到控制台、存储变量或发送到后端
  console.log("纬度:", latitude, "经度:", longitude);

  // 原有代码逻辑...
  geocoder.geocode({ placeId: place.place_id }).then(({ results }) => {
    // 原有代码...
  }).catch((e) => window.alert("Geocoder failed due to: " + e));
});

方式二:从Geocoder返回的结果中提取

如果更依赖Geocoder的返回数据,也可以从results[0].geometry.location中提取经纬度:

geocoder.geocode({ placeId: place.place_id }).then(({ results }) => {
  map.setZoom(11);
  map.setCenter(results[0].geometry.location);
  
  // 提取经纬度
  const latitude = results[0].geometry.location.lat();
  const longitude = results[0].geometry.location.lng();
  console.log("纬度:", latitude, "经度:", longitude);

  // 原有代码逻辑...
  marker.setPlace({
    placeId: place.place_id,
    location: results[0].geometry.location,
  });
  marker.setVisible(true);
  
  // 可选:将经纬度添加到信息窗口显示
  infowindowContent.children["place-coords"].textContent = `纬度: ${latitude}, 经度: ${longitude}`;
  
  infowindow.open(map, marker);
}).catch((e) => window.alert("Geocoder failed due to: " + e));

把经纬度显示到信息窗口(可选)

如果要在点击标记时展示经纬度,需要先在HTML的infowindow-content元素中添加对应节点:

<div id="infowindow-content">
  <span id="place-name" class="title"></span><br>
  <span id="place-id"></span><br>
  <span id="place-address"></span><br>
  <span id="place-coords"></span> <!-- 新增经纬度显示节点 -->
</div>

内容的提问来源于stack exchange,提问作者Ajith Vinayak

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 04:55:28