Detect the nearest transit stop from the given location

会有一股神秘感。 提交于 2019-11-28 15:28:28

Well, you could use the places-API to find the nearest transit-stops, it works fine for me for the given location.

Just do a request with the parameters:

  • location (latlng-object of the given location)
  • radius(radius to search for in meters)
  • types(array of valid types, e.g. ['bus_station','subway_station'])

Checkout the fiddle: http://jsfiddle.net/doktormolle/aZrvs/

For retrieving further details(bus service No, bus stop ID) I don't have any good idea right now.

There should be a way, those data on maps.google.com will be retrieved by using AJAX, so there is a ressource. But as long as there is no public API to fetch those results it would not be legal to use this ressource.

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)

Adding &output=json to your initial query (http://maps.google.com/maps?q=transit%20stop%20near%20New%20Bugis%20Street%20Singapore&output=json), is not a legal way to get this information?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!