NMEA GPS and Google Map in 23 lines of code

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

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

Leave a Comment

*