Google Maps API的使用
2018-06-18 01:58:48来源:未知 阅读 ()
- Directions API
- Distance Matrix API
- Elevation API
- Geocoding API
- Geolocation API
- Time Zone API
- Roads API
- Places API
import googlemaps
from datetime import datetime
gmaps = googlemaps.Client(key='Add Your Key here')
# Geocoding an address
geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
print(geocode_result[0]['geometry']['location'])
# Look up an address with reverse geocoding
reverse_geocode_result = gmaps.reverse_geocode((40.714224, -73.961452))
print(reverse_geocode_result[0]['address_components'][1]['long_name'])
import googlemaps
from datetime import datetime
gmaps = googlemaps.Client(key='Add Your Key here')
# Request directions via public transit
now = datetime.now()
directions_result = gmaps.directions("Sydney Town Hall",
"Parramatta, NSW",
mode="transit",
departure_time=now)
print(directions_result)
import googlemaps
from datetime import datetime
import os
import csv
import pandas as pd
import matplotlib.pyplot as plt
import math
# 将已知的多个txt文件中的内容放到一个CSV文件下
def txt2Csv(dataPath, csvname):
fileList = os.listdir(dataPath)
csvFile = open(dataPath + '\\' + csvname, 'w+')
writer = csv.writer(csvFile)
for fileName in fileList:
with open(dataPath + '\\' + fileName) as fileObj:
lines = fileObj.readlines()
for line in lines:
line = line.split(',')
line[-1] = line[-1][0:-1]
writer.writerow((line))
csvFile.close()
# 根据经纬度获取两地之间的距离及花费的时间
def getDistanceDuration(key, path, csvName):
gmaps = googlemaps.Client(key=key)
df = pd.read_csv(path + '\\' + csvName)
df.columns = ['id', 'time', 'longitude', 'latitude']
durationList = []
distanceList = []
try:
for i in range(1, 1000):
now = datetime.now()
# 调取google API的directions:
directions_result = gmaps.directions((df.iloc[i, 3], df.iloc[i, 2]),
(df.iloc[i+1, 3], df.iloc[i+1, 2]),
mode="driving",
departure_time=now)
# 按照返回的格式,找出distance及duration,追加到列表中并返回
distanceList.append(directions_result[0]['legs'][0]['distance']['value'])
durationList.append(directions_result[0]['legs'][0]['duration']['value'])
except googlemaps.exceptions._RetriableRequest:
pass
return distanceList, durationList
path = 'D:\\Learnning\\python\\scrape\\taxiData\\T-drive Taxi Trajectories\\release\\taxi_log_2008_by_id'
txt2Csv(path, 'geodata.csv')
distanceList, durationList = getDistanceDuration('AIzaSyD8X6tJx6Ap5TVHlqwSso8iTwZfDWcFsOA', path, 'geodata.csv')
# 对返回数据的单位做转换, 并使用math.ceil对数据向上取整
distanceList = [math.ceil(dis/1000) for dis in distanceList]
durationList = [math.ceil(dis/60) for dis in durationList]
totalDistance = 0
totalDuration = 0
# 计算总路程,并画出每段路程的距离在总路程中的占比:
for distance in distanceList:
totalDistance += distance
distancePropo = [distance/totalDistance for distance in distanceList]
plt.bar(distanceList, distancePropo)
plt.title("Distance interval")
plt.xlabel("Km")
plt.ylabel("Proportion")
plt.show()
# 计算总时间,并画出每段路程花费的时间在总时间中的占比:
for duration in durationList:
totalDuration += duration
durationPropo = [duration/totalDuration for duration in durationList]
plt.bar(durationList, durationPropo)
plt.title("Time interval")
plt.xlabel("Min")
plt.ylabel("Proportion")
plt.show()
按行驶距离分布:
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:Python的C拓展简介
- 【Demo 1】基于object_detection API的行人检测 1:环境与依 2019-07-24
- Google实践中总结的Python规范,get了吗? 2019-05-18
- os模块及其API&属性 2019-04-11
- pickle模块及其API 2019-04-11
- 【书评】【不推荐】《TensorFlow:实战Google深度学习框架》 2019-03-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash