Detect the nearest transit stop from the given location

后端 未结 3 688
轮回少年
轮回少年 2020-12-12 12:54

I need to get all the nearby public transit information within certain distance from a given location. The type of public transit can be either bus, train, et

3条回答
  •  时光说笑
    2020-12-12 13:18

    You can still enumerate all of Bus Service Number, Bus Stop ID (Station Names) after getting the google-places-api details, as @Dr.Molle: said.

    Open the webpage of detail['result']['url'], and then XPath the string of bus ID list.

    Below is an example to get Taipei's bus Info around a location (latitude, longitude). More detail implementation see https://github.com/MikimotoH/gisTools/blob/master/google_place.py

    places = get_web_json(
        'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' +
        'key=%s&location=%f,%f' % (apikey, lat, lng) +
        '&rankby=distance&language=zh-TW&types=bus_station')
    if places['status'] == 'OK':
        for result in places['results']:
            placeid = result['place_id']
            detail = get_web_json(
                'https://maps.googleapis.com/maps/api/place/details/' +
                'json?key=%s&placeid=%s' % (apikey, placeid) +
                '&language=zh-TW')
            station = detail['result']['name']
            loc = detail['result']['geometry']['location']
            buspage = get_webpage(detail['result']['url'])
            tree = lxml.html.document_fromstring(buspage)
            bus_elm = tree.xpath("/html/body/div[1]/div/div[4]/div[4]/div/div/div[2]/div/div[2]/div[1]/div[2]/div/div/div[2]/div/table/tr/td")[0]
            buses = list(filter(lambda s: len(s.strip()) > 0,
                                bus_elm.text_content().strip().split()))
            yield (station, float(loc['lat']), float(loc['lat']), buses)
    

提交回复
热议问题