Created
March 17, 2010 17:34
-
-
Save ucnv/335493 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'ubygems' | |
require 'json' | |
require 'open-uri' | |
require 'erb' | |
mail = '[email protected]' | |
pass = 'your-password' | |
data = Struct.new('Foursquare', :user, :days, :icon, :history).new | |
out = './4sqmaps.html' | |
api = 'http://api.foursquare.com/v1/' | |
last_checkin_id = nil | |
open("#{api}user.json", :http_basic_authentication => [mail, pass]) do |f| | |
j = JSON.parse f.read | |
u = j['user'] | |
name = u['firstname'] | |
name += u['lastname'] unless(u['lastname'].nil?) | |
data.user = name | |
data.icon = u['photo'] | |
last_checkin_id = u['checkin']['id'] | |
end | |
sinceid = 0 | |
data.history = [] | |
while(true) do | |
h = open("#{api}history.json?sinceid=#{sinceid}&l=250", | |
:http_basic_authentication => [mail, pass]) do |f| | |
j = JSON.parse f.read | |
if sinceid == 0 | |
d = Time.parse j['checkins'][0]['created'] | |
data.days = ((Time.now - d) / 86400).to_i | |
end | |
j['checkins'].map do |c| | |
sinceid = c['id'] | |
v = c['venue'] | |
[v['name'], c['created'], v['geolat'], v['geolong']] | |
end | |
end | |
data.history += h | |
break if sinceid.to_i >=last_checkin_id.to_i | |
end | |
File.open(out, 'w') do |f| | |
f.write ERB.new(DATA.read).result(binding) | |
end | |
`open #{out}` | |
__END__ | |
<!doctype html> | |
<meta charset="utf-8" /> | |
<title><%= data.user %>'s <%= data.days %> days move</title> | |
<style> | |
html, body, #map { | |
margin: 0; | |
padding: 0; | |
width: 100%; | |
height: 100%; | |
} | |
</style> | |
<script src="http://maps.google.com/maps/api/js?sensor=false"></script> | |
<script> | |
window.onload = function() { | |
var myOptions = { | |
zoom: 3, | |
center: new google.maps.LatLng(30, -180), | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}; | |
var map = new google.maps.Map(document.getElementById("map"), myOptions); | |
var latlngs = [ | |
<% data.history.each do |h| %> | |
new google.maps.LatLng(<%= h[2] %>, <%= h[3] %>), | |
<% end %> | |
]; | |
var path = new google.maps.Polyline({ | |
path: latlngs, | |
strokeColor: '#f0c', | |
strokeOpacity: 1.0, | |
strokeWeight: 1 | |
}); | |
path.setMap(map); | |
//---------------------- | |
var opened = null; | |
<% data.history.each_with_index do |h, i| %> | |
var m<%= i %> = new google.maps.Marker({ | |
position: new google.maps.LatLng(<%= h[2] %>, <%= h[3] %>), | |
map: map, | |
}); | |
google.maps.event.addListener(m<%= i %>, 'click', function() { | |
if(opened != null) opened.close(); | |
opened = new google.maps.InfoWindow({ | |
content: "<%= h[0] %><br /><small>at <%= h[1] %></small>", | |
size: new google.maps.Size(50, 50) | |
}) | |
opened.open(map, m<%= i %>); | |
}); | |
<% end %> | |
} | |
</script> | |
<div id="map"></div> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment