NMEA GPS and Google Map in 23 lines of code
Posted by Sandro Paganotti in
Ruby on Rails -
comments are closed
Today I’ve found this fantastic example: GPS and Google Map with Python in 42 lines of code obviously the first thing I thought was
how can I do this in Ruby?
Well it was quite easy:- first of all I installed the ruby-serialport gem
- then I located the serial port which map my GPS bluetooth device; this procedure could vary depending on which os your working on; in Os X I had to create a serial port from the bluetooth panel ( first click on the device, then click on the “Edit Serial Ports…” button and configure the serial port as shown in the following snapshot ).

- last I rewrite the code I found in ruby; this is the result :
require 'serialport'
def dmmm2dec(degrees,sw)
deg= (degrees/100.0).floor #decimal degrees
frac= ((degrees/100.0)-deg)/0.6 #decimal fraction
ret = deg+frac #positive return value
if ((sw=="S") or (sw=="W")):
ret=ret*(-1) #flip sign if south or west
end
return ret
end
sp = SerialPort.new('/dev/tty.holux', 4800,8,1,SerialPort::NONE)
while(line=sp.readline) do
if line =~ /\$GPGGA/
tokens = line.split(",")
lat = dmmm2dec((tokens[2]).to_f,tokens[3]) #[2] is lat in deg+minutes, [3] is {N|S|W|E}
lng = dmmm2dec((tokens[4]).to_f,tokens[5]) #[4] is long in deg+minutes, [5] is {N|S|W|E}
puts "http://maps.google.com/maps?ie=UTF8&ll=#{lat},#{lng}"
sleep 1
end
end
Sandro.


Comments
Guitar
Posted on July 04
Gio
Posted on July 04
Ilya Grigorik
Posted on July 05
Jason
Posted on July 17
Sandro
Posted on July 17
Max Lapshin
Posted on September 28