Skip to content

Instantly share code, notes, and snippets.

@shokai
Created August 2, 2012 22:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shokai/3241217 to your computer and use it in GitHub Desktop.
Save shokai/3241217 to your computer and use it in GitHub Desktop.
Get your location with WiFi BSSID and GoogleMap
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'json'
def get_addrs
case RUBY_PLATFORM
when /darwin/
lines = `/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -s`.split(/[\r\n]/)
lines.shift
lines.map{|i|
a = i.scan(/[^\s]+/)
{:bssid => a[1], :signal => a[2].to_i}
}
when /linux/
lines = `iwlist wlan0 scan`.split(/[\r\n]/)
addrs = []
lines.each do |line|
addrs.push({:bssid => line.split(/\s+/).last, :signal => 8}) if line =~ /Address: /
addrs.last[:signal] = line.scan(/Signal level=(\-?\d+)/)[0][0].to_i rescue next if line =~ /Signal level=/
end
addrs
end
end
def get_location(addrs)
uri = URI.parse 'http://www.google.com/loc/json'
query = {
:version => '1.1.0',
:host => 'maps.google.com',
:request_address => true,
:address_language => ENV['LANG'].scan(/([^\.]+)/)[0][0],
:wifi_towers => addrs.map{|addr| {:mac_address => addr[:bssid], :signal_strength => addr[:signal], :age => 0} }
}.to_json
res = Net::HTTP.start(uri.host, uri.port).request(Net::HTTP::Post.new(uri.request_uri), query)
raise "Response Error (#{res.code})" unless res.code.to_i == 200
JSON.parse(res.body)['location']
end
p addrs = get_addrs
puts location = get_location(addrs)
puts "https://maps.google.com/?ll=#{location['latitude']},#{location['longitude']}&z=16"
@johnboiles
Copy link

I get the following:

wifi_gmap_location.rb:36:in `get_location': Response Error (404) (RuntimeError)
    from wifi_gmap_location.rb:42:in `<main>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment