本文主要介绍使用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的更多相关文章

  1. Day6 google Geocoding API

    在看机器学习实战中K-means一章,练习中需要调用Yahoo PlaceFinder API 为地点添加经纬度,语言是python.申请到了appid但调用好像还要收费,要填写银行卡号才能用,没管那 ...

  2. python google play

    #!/usr/env python #-*- coding: utf-8 -*- import urllib import urllib2 import random import requests ...

  3. 吴裕雄--天生自然python Google深度学习框架:Tensorflow实现迁移学习

    import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...

  4. 详解Python Google Protocol Buffer

    为什么要使用PB? PB(Protocol Buffer)是 Google 开发的用于结构化数据交换格式,作为腾讯云日志服务标准写入格式.因此用于写入日志数据前,需要将日志原始数据序列化为 PB 数据 ...

  5. 吴裕雄--天生自然python Google深度学习框架:经典卷积神经网络模型

    import tensorflow as tf INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE_SIZE = 28 NUM_CHANNELS = 1 NUM_LABEL ...

  6. 吴裕雄--天生自然python Google深度学习框架:图像识别与卷积神经网络

  7. 吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...

  8. 吴裕雄--天生自然python Google深度学习框架:深度学习与深层神经网络

  9. 吴裕雄--天生自然python Google深度学习框架:TensorFlow实现神经网络

    http://playground.tensorflow.org/

随机推荐

  1. 一个github账户多台电脑代码提交

    在实际工作生活中,我们可能不一定仅仅在一台电脑上编码,比如:我们平时在单位电脑1上写代码,提交代码到github账户,而我们也可能会在在家里的电脑2上继续工作,提交代码,这样就是在不同的电脑上提交代码 ...

  2. git学习2:版本库

    创建版本库 版本库,又称仓库,英文名为repository,版本库内的所有文件都可以被Git管理起来,即每个文件的修改.删除,Git都能跟踪. 1,在目录中创建版本库 在目录中有两种创建版本库的方法, ...

  3. [转]第1讲 什么是EMI滤波器

    转:http://www.murata.com/zh-cn/products/emiconfun/emc/2010/10/28/en-20101028-p1 <前言> EMI是Electr ...

  4. PHP常用函数(1)

    1. create_linkstring 说明: 把数组拼接成字符串 if (!function_exists('create_linkstring')) { /** * 把数组所有元素,按照“参数= ...

  5. IIS7.0上传文件限制的解决方法

    在 Windows7(iis7.5).Win2008(iis 7.0)和Win2003(iis 6.0) 中,默认设置是特别严格和安全的,这样可以最大限度地减少因以前太宽松的超时和限制而造成的攻击. ...

  6. ubuntu12.04 安装mac主题

    1.下载主题包我把主题上传到了csdn的资源栏目,下载地址:http://download.csdn.net/detail/chenjiebin/4531537 2.将/mac/sharc/theme ...

  7. 《Linux企业应用案例精解(第2版)》新书发售啦

    本书在出版当年就获得了不错的销量,同时被中国科学院国家科学图书馆.中国国家图书馆.首都图书馆.清华大学.北京大学等上百所国内综合性大学图书馆收录为馆藏图书,在IT业界赢得了良好的口碑.随后2012年年 ...

  8. SVN外链

    1 外链使用场景 使用Subversion进行版本管理时,有时需要将一些公共库或者开源库链接到自己项目中,为了同时做到与外部库实时更新,使用Subversion的外链功能,从而将外部的库当做本地项目的 ...

  9. clipboard_monitor_in_win7

    添加监听 AddClipboardFormatListener(this.Handle); 移除 RemoveClipboardFormatListener(this.Handle); #region ...

  10. 轻松获取LAMP或LNMP环境编译参数

    查看web服务的编译参数 1. 查看nginx的编译参数: /apps/nginx/sbin/nginx -V 范例: [root@VM- ~]# /apps/nginx/sbin/nginx -V ...