Last active
January 28, 2018 06:42
-
-
Save lizy14/0d0a4bea1477e7c0a378b11af9118e7d to your computer and use it in GitHub Desktop.
Map APIs Aggregation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask import request | |
app = Flask(__name__) | |
BD_KEY = '0123456789abcdef' | |
GD_KEY = '0123456789abcdef' | |
GG_KEY = '0123456789abcdef' | |
import requests | |
import googlemaps | |
gmaps = googlemaps.Client(key=GG_KEY) | |
def translate_duration(m): # seconds | |
minute = int(m) // 60 | |
hour = minute // 60 | |
minute = minute % 60 | |
if hour == 0: | |
return "{} 分钟".format(minute) | |
else: | |
return "{} 小时 {} 分钟".format(hour, minute) | |
# duration estimation | |
def get_gd_eta(lon, lat, dst): | |
def get_gd_loc_by_str(dst): | |
r = requests.get('http://restapi.amap.com/v3/geocode/geo', timeout=5, params={ | |
'key': GD_KEY, | |
'output': 'json', | |
'address': dst | |
}) | |
result = r.json()['geocodes'] | |
return result[0]['location'] | |
def get_gd_loc_by_gps(loc): | |
r = requests.get('http://restapi.amap.com/v3/assistant/coordinate/convert', timeout=5, params={ | |
'locations': loc, | |
'coordsys': 'gps', | |
'output': 'json', | |
'key': GD_KEY | |
}) | |
result = r.json()['locations'] | |
return ','.join("{:.6f}".format(float(d)) for d in result.split(',')) | |
origin = get_gd_loc_by_gps("{},{}".format(lon, lat)) | |
destination = get_gd_loc_by_str(dst) | |
r = requests.get('http://restapi.amap.com/v3/direction/transit/integrated', timeout=5, params={ | |
'key': GD_KEY, | |
'city': '010', | |
'output': 'json', | |
'origin': origin, | |
'destination': destination | |
}) | |
result = r.json()['route']['transits'] | |
durations = [t['duration'] for t in result] | |
return translate_duration(min(durations)) | |
def get_gg_eta(lon, lat, dst): | |
result = gmaps.directions( | |
(float(lat), float(lon)), | |
dst, | |
mode="transit", | |
language="zh-CN" | |
) | |
return ','.join([translate_duration(route['legs'][0]['duration']['value']) for route in result]) | |
def get_bd_eta(lon, lat, dst): | |
def get_bd_loc_by_gps(lon, lat): | |
r = requests.get('http://api.map.baidu.com/geoconv/v1/', timeout=5, params={ | |
'ak': BD_KEY, | |
'coords': "{},{}".format(lon, lat) | |
}) | |
result = r.json()['result'][0] | |
return "{:.6f},{:.6f}".format(float(result['y']), float(result['x'])) | |
def get_bd_loc_by_str(dst): | |
r = requests.get('http://api.map.baidu.com/geocoder/v2/', timeout=5, params={ | |
'ak': BD_KEY, | |
'city': '北京市', | |
'output': 'json', | |
'address': dst | |
}) | |
result = r.json()['result']['location'] | |
return "{:.6f},{:.6f}".format(float(result['lat']), float(result['lng'])) | |
origin = get_bd_loc_by_gps(lon, lat) | |
destination = get_bd_loc_by_str(dst) | |
r = requests.get('http://api.map.baidu.com/direction/v2/transit', timeout=5, params={ | |
'ak': BD_KEY, | |
'origin': origin, | |
'destination': destination | |
}) | |
result = r.json() | |
result = result['result']['routes'] | |
durations = [t['duration'] for t in result] | |
return translate_duration(min(durations)) | |
# reverse geo-encoding | |
def get_bd_addr(lon, lat): | |
r = requests.get('http://api.map.baidu.com/geocoder/v2/', timeout=5, params={ | |
'coordtype': 'wgs84ll', | |
'location': '{},{}'.format(lat, lon), | |
'ak': BD_KEY, | |
'output': 'json' | |
}) | |
result = r.json()['result'] | |
keys = ['formatted_address', 'sematic_description'] | |
return ';'.join([result[k] for k in keys]) | |
def get_gg_addr(lon, lat): | |
result = gmaps.reverse_geocode( | |
(float(lat), float(lon)), | |
language="zh-CN" | |
) | |
return result[0]['formatted_address'] | |
def get_gd_addr(lon, lat): | |
r = requests.get('http://restapi.amap.com/v3/geocode/regeo', timeout=5, params={ | |
'key': GD_KEY, | |
'location': '{},{}'.format(lon, lat), | |
'output': 'json' | |
}) | |
result = r.json()['regeocode'] | |
return result['formatted_address'] | |
# query handler | |
@app.route('/location') | |
def hello(): | |
lon = request.args.get('lon', '') | |
lat = request.args.get('lat', '') | |
lines = [] | |
for get_addr in [get_bd_addr, get_gd_addr, get_gg_addr]: | |
try: lines.append(get_addr(lon, lat)) | |
except: pass | |
return '<br/>\n'.join(lines) | |
@app.route('/eta') | |
def world(): | |
lon = request.args.get('lon', '') | |
lat = request.args.get('lat', '') | |
lines = [] | |
for dst in ["清华附中", "北京师范大学东门"]: | |
lines.append("ETA to {}: ".format(dst)) | |
for get_eta in [get_gd_eta, get_gg_eta, get_bd_eta]: | |
try: lines.append(get_eta(lon, lat, dst)) | |
except: pass | |
return '<br/>\n'.join(lines) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests | |
flask | |
googlemaps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment