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

如何使用Google Maps Python API创建可共享的地点列表

可以用Google Maps相关API实现可共享的地点列表

你当前用的google-maps-services-python库主要处理静态地图、地理编码这类Web Service接口,但要创建能在Google Maps里展示、还能通过URL共享的地点列表(就像你举的自定义美食地图那种),得结合Google Drive API或者Maps JavaScript API来做,下面是两种可行的实现方案:


方案一:通过Google Drive API创建My Maps自定义地图

My Maps里的自定义地图本质是存在Google Drive里的KML格式文件,你可以用Google Drive API创建这类文件,写入地点标记数据,再生成共享链接给其他人访问。

操作步骤

  1. 先在Google Cloud控制台启用Google Drive API,生成服务账号密钥文件
  2. 用google-api-python-client库创建My Maps文件,写入包含地点的KML内容
  3. 设置文件的共享权限,生成可公开访问的URL

示例代码

from googleapiclient.discovery import build
from google.oauth2 import service_account
import io

# 替换成你的服务账号密钥文件路径
SERVICE_ACCOUNT_FILE = 'your-service-account-key.json'
SCOPES = ['https://www.googleapis.com/auth/drive']

# 初始化Drive API客户端
creds = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)
drive_service = build('drive', 'v3', credentials=creds)

# 构建KML内容,包含三个地点标记
kml_content = """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>我的自定义地点列表</name>
    <description>API创建的共享地图</description>
    <Placemark>
      <name>地点S</name>
      <description>坐标:62.107733, -145.541936</description>
      <Point>
        <coordinates>-145.541936,62.107733,0</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <name>Delta Junction, AK</name>
      <description>阿拉斯加的小镇</description>
      <Point>
        <coordinates>-145.742778,64.068333,0</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <name>地点C</name>
      <description>Tok, AK</description>
      <Point>
        <coordinates>-142.987222,63.259722,0</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>"""

# 创建My Maps格式的文件(MIME类型固定为application/vnd.google-earth.kml+xml)
file_metadata = {
    'name': '我的地点列表',
    'mimeType': 'application/vnd.google-earth.kml+xml'
}
media = io.BytesIO(kml_content.encode('utf-8'))
file = drive_service.files().create(
    body=file_metadata,
    media_body=media,
    fields='id'
).execute()

# 设置文件为公开可读
drive_service.permissions().create(
    fileId=file.get('id'),
    body={'type': 'anyone', 'role': 'reader'}
).execute()

# 生成共享链接
share_url = f"https://www.google.com/maps/d/u/0/viewer?mid={file.get('id')}"
print(f"共享链接:{share_url}")

方案二:用Maps JavaScript API做可共享的自定义地图网页

你可以写一个简单的HTML页面,用Maps JavaScript API加载地图并添加标记,把这个页面托管到静态网站平台(比如GitHub Pages、Netlify),分享页面URL就能让别人在浏览器里看你的地点列表。

示例HTML代码

<!DOCTYPE html>
<html>
<head>
    <title>我的共享地点地图</title>
    <script src="https://maps.googleapis.com/maps/api/js?key=你的API密钥"></script>
    <script>
        function initMap() {
            // 地图中心坐标
            const center = new google.maps.LatLng(63.259591, -144.667969);
            const map = new google.maps.Map(document.getElementById('map'), {
                zoom: 6,
                center: center,
                mapTypeId: 'hybrid'
            });

            // 地点标记数据
            const markers = [
                {pos: new google.maps.LatLng(62.107733, -145.541936), label: 'S', color: 'blue', title: '地点S'},
                {pos: new google.maps.LatLng(64.068333, -145.742778), label: '', color: 'green', title: 'Delta Junction, AK'},
                {pos: new google.maps.LatLng(63.259722, -142.987222), label: 'C', color: '#FFFF00', title: 'Tok, AK'}
            ];

            // 给地图添加所有标记
            markers.forEach(data => {
                new google.maps.Marker({
                    position: data.pos,
                    map: map,
                    label: data.label,
                    icon: {
                        path: google.maps.SymbolPath.CIRCLE,
                        fillColor: data.color,
                        fillOpacity: 1,
                        strokeColor: '#000',
                        strokeWeight: 1,
                        scale: data.label ? 10 : 5
                    },
                    title: data.title
                });
            });
        }
    </script>
</head>
<body onload="initMap()">
    <div id="map" style="width: 100%; height: 800px;"></div>
</body>
</html>

把代码里的你的API密钥换成你自己的Google Maps API密钥,然后把这个HTML文件传到静态托管平台,分享页面链接就行。


注意点

  • 方案一的服务账号要在Google Cloud控制台配置好Drive的访问权限,不然创建或修改文件会报错
  • 方案二的API密钥要设置正确的域名限制,避免被滥用
  • 两种方案生成的链接都能直接在浏览器打开,显示带标记的地图

内容的提问来源于stack exchange,提问作者Victoria K

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 22:11:01