本文主要介绍使用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. JS中两个感叹号!!的意义

    var foo = true; console.log(!foo); // false console.log(!!foo); // true 两个 !! 就是 非非 如果值为boolean值 两个感 ...

  2. jquery mobile 登陆后页面验证

    调用 pagebeforechange方法 $(document).bind("pagebeforechange", function (e, data) { if (typeof ...

  3. CryptoJS遇到的小坑

    CryptoJs是前端加密用的,具体介绍:http://code.google.com/p/crypto-js/ 对应国内免费CDN 服务:http://www.bootcdn.cn/crypto-j ...

  4. XE6 & IOS开发之开发者账号、苹果证书(3):关于在XE6中使用苹果证书的简单介绍

    网上能找到的关于Delphi XE系列的移动开发的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 1.关于在XE6中使用苹 ...

  5. iOS设计模式笔记

    设计模式 所谓设计模式,就是经过无数程序员的努力,在很多代码的基础上抽取出来的"模式" 一种设计模式,通常用来解决一种问题 MVC体系架构 也是一个设计模式,主要解决:界面(V), ...

  6. .Net 连接字符串的解释

    https://msdn.microsoft.com/zh-cn/library/cc716756.aspx 连接字符串参数 连接字符串的格式是使用分号分隔的键/值参数对列表: keyword1=va ...

  7. QTP参数化

    使用QTP可以通过将固定值替换为参数,扩展测试范围,可以提高测试的灵活性. 在QTP中,我们可以对以下部分进行参数化: 1.对象属性参数化 2.对象名称的参数化 3.对象方法的参数的参数化

  8. JavaScript笔记:变量及其作用域

    一.变量的定义及声明 在javascript中变量仅仅是用来保存值的一个占位符而已,定义变量时要使用关键字var后跟一个变量名,如下所示: var message; //定义一个变量message,像 ...

  9. hrbrid需要做的

    1 返回并刷新 A webveiw push 到 B webview.当由B返回到A时候, A需要刷新页面.

  10. STL之序列容器deque

    首先看看deque的模板声明: template <class T,  class Alloc = allocator<T>>  // 原本还有个参数BufSize,现在新版本 ...