Python+Google Geocoding
本文主要介绍使用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+Google Geocoding的更多相关文章
- Day6 google Geocoding API
在看机器学习实战中K-means一章,练习中需要调用Yahoo PlaceFinder API 为地点添加经纬度,语言是python.申请到了appid但调用好像还要收费,要填写银行卡号才能用,没管那 ...
- python google play
#!/usr/env python #-*- coding: utf-8 -*- import urllib import urllib2 import random import requests ...
- 吴裕雄--天生自然python Google深度学习框架:Tensorflow实现迁移学习
import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...
- 详解Python Google Protocol Buffer
为什么要使用PB? PB(Protocol Buffer)是 Google 开发的用于结构化数据交换格式,作为腾讯云日志服务标准写入格式.因此用于写入日志数据前,需要将日志原始数据序列化为 PB 数据 ...
- 吴裕雄--天生自然python Google深度学习框架:经典卷积神经网络模型
import tensorflow as tf INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE_SIZE = 28 NUM_CHANNELS = 1 NUM_LABEL ...
- 吴裕雄--天生自然python Google深度学习框架:图像识别与卷积神经网络
- 吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...
- 吴裕雄--天生自然python Google深度学习框架:深度学习与深层神经网络
- 吴裕雄--天生自然python Google深度学习框架:TensorFlow实现神经网络
http://playground.tensorflow.org/
随机推荐
- WIN7 OEM Nin1 地址
WIN7 SP1 OEM Nin1 201205 一.映像版本列表 Windows 7 旗舰版 32位 LENOVOWindows 7 旗舰版 32位 ASUSWindows 7 旗舰版 32位 AC ...
- XCodeGhost 笔记
因为服务已经关掉了,所以要改路由Openwrt vi /etc/config/dhcp vi /etc/dnsmasq/dnsmasq.conf /etc/init.d/dnsmasq restart ...
- Android 常用控件的介绍
http://www.cnblogs.com/linjiqin/category/284058.html 最流行的android组件大全:http://www.cnblogs.com/linjiqin ...
- Android Studio in OSX 提高工作效率的快捷键
前言 本篇文章参考了<倍数提高工作效率的Android Studio>一文,快捷键基于OS X系统. OS X Yosemite 10.10.5 Android Studio 1.3.1 ...
- 安卓手机屏幕录像之scr
打开SCR Screen Recorder,屏幕会显示录像控制面板,点击“开始”按钮就可以开始录像: - 停止录像的方法有两种.一种是锁屏,锁屏后等待2秒,录像文件会自动保存到SD卡,另外一种是重新打 ...
- Mysql常见四种索引的使用
提到MySQL优化,索引优化是必不可少的.其中一种优化方式 --索引优化,添加合适的索引能够让项目的并发能力和抗压能力得到明显的提升. 我们知道项目性能的瓶颈主要是在"查(select)&q ...
- H5新出的flex布局
百度前端技术学院第一阶段中的任务十,就是关于flexbox布局的 与flexbox布局相关的资料如下: 1.flex布局教程-语法篇-阮一峰的网络日志 http://www.ruanyifeng.c ...
- Redis安装手册
转载请注明出处:http://www.cnblogs.com/robinjava77/p/5465146.html (Robin) 1)下载redis:wget http://download.red ...
- oracle修改字段长度
alter table 表名 modify (字段名 字段类型长度);alter table cachemsg modify (callernum varchar(40));
- python模块与包的导入
1. 模块与包的区别 模块,即module,一个包含python语句的.py文件就是一个模块!每个源代码文件都会自动成为模块!没有额外的语法用来声明模块. 包,又称模块包,即module packag ...