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

关于Google Maps JavaScript API无跳转保存地点功能的技术咨询

解决Google Maps不跳转保存地点的问题

我来帮你理清这个问题——你遇到的情况核心原因是signed-in功能已完全废弃,而且官方旧示例里的说明已经过时,导致出现了误导性的信息。

为什么旧示例只显示“View On Google Maps”?

原来的Maps JavaScript API 3.27及更早版本,在启用signed_in=true并通过Place对象创建标记时,确实会自动在InfoWindow里添加“Save to Google Maps”链接。但从3.28版本开始,signed-in功能被彻底移除了,所以即使你在脚本里指定v=3.27和signed_in=true,现在的API服务也不再支持这个特性,只会显示跳转至Google Maps的链接。

如何实现不跳转的“保存地点”功能?

现在官方推荐使用SaveWidget来实现原地保存地点的功能,不需要跳转至Google Maps。你需要手动初始化这个控件,绑定到页面的某个元素(比如按钮),用户点击后会弹出Google的保存界面,完成操作后留在当前页面。

修改后的示例代码

<!DOCTYPE html>
<html>
<head>
  <title>Save a Place</title>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  <meta charset="utf-8">
  <style>
    #map { height: 80%; }
    html, body { height: 100%; margin: 0; padding: 0; }
    #save-button {
      position: absolute;
      bottom: 20px;
      left: 20px;
      padding: 10px;
      background: white;
      border: 1px solid #ccc;
      border-radius: 4px;
      cursor: pointer;
      font-size: 14px;
    }
  </style>
  <script>
    let map;
    // 替换为目标地点的Place ID(可通过Google Places平台工具查询)
    const targetPlaceId = 'ChIJN1t_tDeuEmsRUsoyG83frY4'; 

    function initMap() {
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 17,
        center: {lat: -33.8666, lng: 151.1958}
      });

      // 创建地图标记
      const marker = new google.maps.Marker({
        map: map,
        position: {lat: -33.8666, lng: 151.1958}
      });

      // 初始化SaveWidget实现原地保存
      const saveWidget = new google.maps.SaveWidget({
        placeId: targetPlaceId,
        apiKey: 'YOUR_API_KEY', // 替换成你的API密钥
        element: document.getElementById('save-button'),
        // 保存状态变化时的回调函数
        onSaveChange: (isSaved) => {
          document.getElementById('save-button').textContent = isSaved 
            ? '已保存到Google Maps' 
            : '保存到Google Maps';
        }
      });

      // 绑定信息窗口
      const infoWindow = new google.maps.InfoWindow({
        content: 'Google Sydney'
      });

      marker.addListener('click', () => {
        infoWindow.open(map, marker);
      });
    }
  </script>
</head>
<body>
  <div id="map"></div>
  <button id="save-button">保存到Google Maps</button>
  <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>
</body>
</html>

关键注意点

  • 你需要先获取目标地点的Place ID,可以通过Google Places API或Maps平台的Place ID查找工具获取
  • SaveWidget需要用户已登录Google账号,未登录时会引导用户完成登录,整个流程都在当前页面完成
  • 不再需要signed_in=true参数,这个参数已经从API中移除

关于官方文档的误导性

你提到的旧示例里的注释确实是过时的,官方没有及时更新旧示例的说明,导致开发者误解。现在的Maps JavaScript API已经不再支持通过InfoWindow自动添加保存链接,必须手动集成SaveWidget来实现原地保存功能。

内容的提问来源于stack exchange,提问作者Andrew Schultz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:44:43