Google Maps Platform 兼容替换
Geocoding、Places Autocomplete、Time Zone 和 Elevation 网络服务都在 gapi.mygeocode.com 上的相同路径响应,JSON 结构与 Google 返回的相同。更换主机,把您的 My Geocode 密钥放入 key 参数,其余代码保持原样即可。
只需更换一个主机名
| 原始主机 |
|
|---|---|
| 改用 | gapi.mygeocode.com |
| 密钥参数 | key(可选:任何地址不使用密钥每天也有 2,500 次请求;如需使用额度、套餐和使用记录,请在此填入您的 My Geocode 密钥) |
之前
curl "https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_GOOGLE_KEY"之后
curl "https://gapi.mygeocode.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_MYGEOCODE_KEY"端点
| gapi.mygeocode.com 上的路径 | 功能 | 底层服务 |
|---|---|---|
/maps/api/geocode/json?address=... | 正向地理编码 | 正向地理编码 |
/maps/api/geocode/json?latlng=lat,lng | 逆地理编码 | 逆地理编码 |
/maps/api/place/autocomplete/json?input=... | Places Autocomplete(带坐标的预测结果) | 地址自动补全 |
/maps/api/timezone/json?location=lat,lng×tamp=... | Time Zone API(时区) | 时区查询 |
/maps/api/elevation/json?locations=lat,lng|lat,lng | Elevation API | 海拔查询 |
响应示例
这是上述请求返回的内容。字段名、嵌套和类型与 Google Maps 一致。只有背后的数据来自我们。
来自 gapi.mygeocode.com 的响应
{
"results": [
{
"address_components": [
{ "long_name": "1600", "short_name": "1600", "types": ["street_number"] },
{ "long_name": "Amphitheatre Parkway", "short_name": "Amphitheatre Pkwy", "types": ["route"] },
{ "long_name": "Mountain View", "short_name": "Mountain View", "types": ["locality", "political"] },
{ "long_name": "Santa Clara County", "short_name": "Santa Clara County", "types": ["administrative_area_level_2", "political"] },
{ "long_name": "California", "short_name": "CA", "types": ["administrative_area_level_1", "political"] },
{ "long_name": "United States", "short_name": "US", "types": ["country", "political"] },
{ "long_name": "94043", "short_name": "94043", "types": ["postal_code"] }
],
"formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry": {
"location": { "lat": 37.4224428, "lng": -122.0842467 },
"location_type": "ROOFTOP",
"viewport": {
"northeast": { "lat": 37.4237918, "lng": -122.0828977 },
"southwest": { "lat": 37.4210938, "lng": -122.0855957 }
}
},
"place_id": "mg.us.addr.5e1b2c7a9f",
"plus_code": { "compound_code": "CWC8+W5 Mountain View, CA, USA", "global_code": "849VCWC8+W5" },
"types": ["street_address"]
}
],
"status": "OK"
}细节与差异
每天前 2,500 次请求无需密钥。如需使用 My Geocode 密钥,请将其放在原来 Google 密钥的位置:key 查询参数,或 X-Goog-Api-Key 请求头。X-API-Key 请求头、Authorization: Bearer 和 HTTP Basic 认证(以密钥作为用户名)也适用于每个主机,因此以自有方式进行认证的客户端库无需任何修改即可继续工作。
- 错误正文与您调用的 API 一致:Time Zone API 返回
errorMessage和status,其他 API 返回error_message、空结果列表,然后是status。 - 与 Google 一样,时区响应在开阔水域上返回
ZERO_RESULTS,并遵循language参数。 - 海拔接口接受
locations、enc:编码的折线,以及带samples的path,最多 512 个坐标点,与 Google 允许的一致;在这里每个坐标点计为一个请求。
如果您的客户端依赖于此处未列出的字段或行为,请告诉我们。兼容性缺口通常很小,因此会很快修复。