We wrote last week about the launch of Barclays Cycle Hire, London’s first public bike rental scheme, and produced a map using data published by the London Datastore, along with some ideas on usability compared with the official map of the docking stations.
The achillesĀ heel of this effort, of course, was the lack of real time location and availability data. While an official API is no doubt on the way, Adrian Short has just published his unofficial Boris Bikes API that loads the official map and then scrapes its status data. We’ve used Adrian’s clever parsing of the official map page to drop live data into a second version of our map, which you can see below. The new map still uses v2 of the Google Maps API, so it won’t play too nicely on iPhones and Androids, but adding the live data’s a useful step. Let us know what you think…
Source Code
The geo.me platform is written in Rails, so we added a controller method that downloads the official map page, uses Adrian’s regex to parse the data, then returns the data in JSON format. Here it is:
def boris_cycle_data
# call the official map and parse the live availability date
# inspired by http://github.com/adrianshort/borisapi
uri = URI.parse("https://web.barclayscyclehire.tfl.gov.uk/maps")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
contents = response.body
# Adrian's part...
regexp = /\{id:"(\d+)".+?name:"(.+?)".+?lat:"(.+?)".+?long:"(.+?)".+?nbBikes:"(\d+)".+?nbEmptyDocks:"(\d+)".+?installed:"(.+?)".+?locked:"(.+?)".+?temporary:"(.+?)"\}/
hour = /var hour='(\d\d:\d\d)'/.match(contents)
matches = contents.scan(regexp)
timestamp = hour[0].split('\'').second
# now process the data, building an array of returned JSON records...
hirelocations = Array.new
for station in matches
# collect attributes from scraped data
id = station[0].to_s
location = station[1]
lat = station[2].to_f
lng = station[3].to_f
bikes_available = station[4].to_i
capacity = bikes_available + station[5].to_i
# let's only list operational sites
if station[6] == "true"
hirelocations.push({
:id => id,
:location => location,
:lat => lat,
:lng => lng,
:bikes_available => bikes_available,
:capacity => capacity,
:timestamp => timestamp
})
end
end
# create our returned JSON data
result={ :data=>hirelocations }
render :text=>result.to_json
end

[...] and now this http://www.geo.me/blog/111/london-cy…map-live-data/ [...]
Good work, and thanks a lot, this is much better than the official site.
One comment: currently the problem is not usually finding a bike, but finding a space.
Could the code be changed so that the flags be three different colours? One for no bike, one for no spaces and one for all other states?
Thanks, Robert, nice idea. We’ve updated the map config to do just that!
[...] London Cycle Hire Map [...]
This is great. Essential given how variable the availability of bikes/docking-slots can be. and much better than the TFL map which–rather pathetically–has never worked on any PC I have used since it was launched. If you derive satisfaction from staring at a blank square, here it is: https://web.barclayscyclehire.tfl.gov.uk/maps. Well done and thanks!