这两天工作需要调用到Google API,所以查询官方文档了解了一下,发现很方便。
1、首先需要注册一个Google Cloud 账号。
2、创建一个项目。
具体步骤不记得了,之后再补,之前碰巧有注册过账号,就可以直接用。项目任意命名就可以。
Google全部API的地址:https://cloud.google.com/maps-platform/?hl=zh-CN
Google地图API的地址:https://cloud.google.com/maps-platform/?apis=maps
1、点击使用入门。
2、之前已经创建过project,直接选择一个即可
3、接着会给一个Google Key,快速复制下来保存即可。这个Key会在后面调用API的时候用到。
首先还是参考自官方文档:https://developers.google.com/maps/documentation/geocoding/start
适用平台:
可以作为Google Maps API的一部分使用,也可以与Java、Python、Go等客户端使用,还可以在服务端使用
官方原话:This service is also available as part of the client-side Google Maps JavaScript API, or for server-side use with the Java Client, Python Client, Go Client and Node.js Client for Google Maps Services.
用途:
Geocoding:将地址转换为地理坐标即经纬度的过程。
Reverse geocoding :将地理坐标转换为实际地址的过程。
Geocoding API也可以通过place ID查找地理位置。
官方原话:
Geocoding is the process of converting addresses (like a street address) into geographic coordinates (like latitude and longitude), which you can use to place markers on a map, or position the map.
Reverse geocoding is the process of converting geographic coordinates into a human-readable address.
You can also use the Geocoding API to find the address for a given place ID.
1、通过HTTP调用查询地理信息接口:会返回json格式的详细地理信息,比如国家Country、省State、城市City、纬度lat、经度lng
https://maps.googleapis.com/maps/api/geocode/json
示例:浏览器直接请求该接口查询1600 Amphitheatre Parkway, Mountain View, CA这个地址(其实是Google美国的地址)的详细信息,注意YOUR_API_KEY需要替换为自己的Google Key
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
返回的数据为:
{
"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.4267861,
"lng" : -122.0806032
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4281350802915,
"lng" : -122.0792542197085
},
"southwest" : {
"lat" : 37.4254371197085,
"lng" : -122.0819521802915
}
}
},
"place_id" : "ChIJtYuu0V25j4ARwu5e4wwRYgE",
"plus_code" : {
"compound_code" : "CWC8+R3 Mountain View, California, United States",
"global_code" : "849VCWC8+R3"
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
其中:
“geometry”:经纬度
“address_components” :按照等级描述了该位置的行政地理信息,
“formatted_address”:格式化好的地理信息。
假设一些原因导致无法访问到接口,也可以将域名https://maps.googleapis.com 改为 https://ditu.google.cn
2、通过HTTP调用查询地理信息接口:会返回json格式的详细地理信息,比如国家Country、省State、城市City、纬度lat、经度lng
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
如果希望返回的语言不同,还可以加上参数&language=en
返回的数据为:
{
"plus_code" : {
"compound_code" : "P27Q+MC New York, NY, USA",
"global_code" : "87G8P27Q+MC"
},
"results" : [
{
"address_components" : [
{
"long_name" : "279",
"short_name" : "279",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Avenue",
"short_name" : "Bedford Ave",
"types" : [ "route" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "279 Bedford Ave, Brooklyn, NY 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.7142484,
"lng" : -73.9614103
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.71559738029149,
"lng" : -73.9600613197085
},
"southwest" : {
"lat" : 40.71289941970849,
"lng" : -73.96275928029151
}
}
},
"place_id" : "ChIJT2x8Q2BZwokRpBu2jUzX3dE",
"plus_code" : {
"compound_code" : "P27Q+MC Brooklyn, New York, United States",
"global_code" : "87G8P27Q+MC"
},
"types" : [
"bakery",
"cafe",
"establishment",
"food",
"point_of_interest",
"store"
]
},
... Additional results truncated in this example[] ...
],
"status" : "OK"
}
其中:
long_name/short_name:各种涉及到的位置的名称
types:对country、state、city这些的定义