摩拜单车地图显示的红点是什么_找到最近的摩拜单车——高德地图API的应用

import folium

import subprocess

import requests

from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

key = 'efb66f1d0e0a897af3702fdb233c0812'

def geoMap(address=u'北京市', city=''):

'''

由地名获得对应经纬度

'''

geoMapUrl = ('http://restapi.amap.com/v3/geocode/geo?'

'key={key}&address={address}&city={city}')

geo_map_url = geoMapUrl.format(key=key, address=address, city=city)

r = requests.get(geo_map_url)

try:

locat = r.json()['geocodes'][0]['location']

lng, lat = locat.split(',')

return lat, lng # 纬度,经度!

except:

raise Exception(u'无法找到该位置, 请尝试更详细的地址, 如添加省市限定')

def get_nearby_bikes(lat, lng):

'''

获取附近的摩拜单车,参考http://www.jianshu.com/p/8662de6f0d7a

'''

mobike_url = "https://mwx.mobike.com/mobike-api/rent/nearbyBikesInfo.do"

headers = {

'referer': "https://servicewechat.com/",

# 'host': "mwx.mobike.com", # host和agent暂时来看没有影响

# 'user-agent': "MicroMessenger/6.5.4.1000 NetType/WIFI Language/zh_CN"

}

data = { # 请求参数: 纬度,经度!

'latitude': lat,

'longitude': lng,

}

r = requests.post(mobike_url, data=data, headers=headers,

timeout=5, verify=False)

return r.json()

def routeMap(origin, destination):

'''

以经纬度起点和终点获取导航路径json数据

'''

routeMapUrl = ('http://restapi.amap.com/v3/direction/walking?'

'key={key}&'

'origin={origin[1]},{origin[0]}&'

'destination={destination[1]},{destination[0]}')

route_map_url = routeMapUrl.format(key=key,

origin=origin, destination=destination)

r = requests.get(route_map_url)

return r.json()

def json_to_line(json_):

'''

将获取的导航json数据转换为[[int, int], [int, int]]形式的经纬度列表

'''

if json_.get('route', None):

line = []

route = json_['route']

dest = route['destination']

ori = route['origin']

line.append(ori)

for step in route['paths'][0]['steps']:

line.extend(step['polyline'].split(';'))

line.append(dest)

line = [lnglat.split(',')[::-1] for lnglat in line]

line = [[float(lat), float(lng)] for lat, lng in line]

return line

return None

def display_map(where, whereLatLng, markers, html_name):

'''

显示所有摩拜单车的地点,并画出从当前点到最近的摩拜的导航路线

'''

tileset = ('https://webrd01.is.autonavi.com/appmaptile?'

'lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}')

# colors = ['green', 'red', 'blue']

map = folium.Map(location=whereLatLng,

zoom_start=7,

tiles=tileset,

attr='附近摩拜单车分布 @ Mobike & Gaode')

for marker in markers:

# import random

# color = random.choice(colors)

folium.Marker(marker,

popup=where,

icon=folium.Icon(color='green')).add_to(map)

json_ = routeMap(whereLatLng, markers[0])

line = json_to_line(json_)

folium.PolyLine(line, color='red').add_to(map)

map.fit_bounds(markers)

map.save(html_name)

command = ['start', html_name]

subprocess.run(command, shell=True)

where = u'西安市钟楼公交站'

whereLatLng = geoMap(where)

json_ = get_nearby_bikes(*whereLatLng)

if json_.get('object', None):

bikes = json_['object']

markers = [(bike['distY'], bike['distX']) for bike in bikes]

html_name = 'mapBike.html'

display_map(where, whereLatLng, markers, html_name)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值