首页 > 代码库 > Python查询一个城市的谷歌地图的经度和纬度
Python查询一个城市的谷歌地图的经度和纬度
Python查询一个城市的谷歌地图的经度和纬度
设置Python环境
shou#!/usr/bin/env python
编写脚本
import argparse
import os
import urllib
import requests
ERROR_STRING = ‘’
def find_lat_long(city):
“”” Find geographic coordinates “””
# Encode query string into Google maps URL
#url = ‘https://www.google.com/maps/place/’ + urllib.quote(city)
response = requests.get(‘https://maps.googleapis.com/maps/api/geocode/json?address=’ + urllib.quote(city))
resp_json_payload = response.json()
# Get XML location from Google maps
print(resp_json_payload[‘results’][0][‘geometry’][‘location’])
# Strip lat/long coordinates from XML
if __name__ == ‘__main__’:
parser = argparse.ArgumentParser(description=’City Geocode Search’)
parser.add_argument(‘–city’, action=”store”, dest=”city”, required=True)
given_args = parser.parse_args()
# print “Finding geographic coordinates of %s” %given_args.city
find_lat_long(given_args.city)
保持并运行脚本
root@ubuntu:~/python# python 6_2_googlemap.py –city “New York”
{u’lat’: 40.7127837, u’lng’: -74.0059413}
root@ubuntu:~/python#
本文出自 “瑞航启程--下一代企业应用” 博客,谢绝转载!
Python查询一个城市的谷歌地图的经度和纬度