首页 技术 正文
技术 2022年11月20日
0 收藏 409 点赞 2,533 浏览 1944 个字

  本文主要介绍使用Python调用Google Geocoding API进行地址到地理坐标的转换。

  Google Geocoding参考https://developers.google.com/maps/documentation/geocoding/?hl=zh-CN

  Google Geocoding API 目前最新版为 Geocoding API (V3)

  要通过 HTTPS 访问 Geocoding API,请使用以下形式:

  HTTP://maps.googleapis.com/maps/geocode/out?parameters

  这里out参数指定请求返回的数据的格式,可以是json或者xml两者之一,本文使用json进行数据获取。本例子中取官方文档中的地址进行解析:

1600 Amphitheatre Parkway, Mountain View, CA,相应的json响应为:

{
"results" : [
{
"address_components" : [
{
"long_name" : "",
"short_name" : "",
"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" : "",
"short_name" : "",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4219998,
"lng" : -122.0839596
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4233487802915,
"lng" : -122.0826106197085
},
"southwest" : {
"lat" : 37.4206508197085,
"lng" : -122.0853085802915
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}

其中json数据中包含两个键results和status。

Demo源码:

 import urllib.request
import json,io,os,base64
#获得json数据
resquest = urllib.request.urlopen('http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false')
data = resquest.read().decode('utf-8')
jsonData = json.loads(data)
results = jsonData['results'] #从json文件中读取results的值(形式为列表)
address = results[0]['formatted_address']
lat_lng = results[0]['geometry']['location']
print(address,'的经纬度为',lat_lng)

results是一个只包含一个元素的元组,该元素(results[0])又是一个json格式的数据,包含4个键address_components、formatted_address、geometry和types。我们这里需要的地理坐标就在geometry中。


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774