Ruby On Rails, Design, Simplicity, Web 2.0, Ajax, Mac and Tons of Pizza.

Jul 04

NMEA GPS and Google Map in 23 lines of code

Posted by Sandro Paganotti in Ruby on Rails - comments are closed digg this add to delicious

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

    Thank you! Helped!
  • Gio

    Posted on July 04

    Really cool!!
  • Ilya Grigorik

    Posted on July 05

    Mmm, awesome! Looking forward to playing with this.
  • Jason

    Posted on July 17

    what gps receiver are you using? Is there one for macbook pro that can fit in the expansion slot?
  • Sandro

    Posted on July 17

    Hi Jason! I'm actually using a HOLUX GPSlim 240 and, I'm sorry, I don't know if there is a GPS receiver that can fit in a MacBook Pro expansion slot (I've a MacBook) Sandro
  • Max Lapshin

    Posted on September 28

    Yes, it is more simple, than my choice (ruby-gps gem), but I wanted to experiment with Ragel.

Post a comment

Categories:

Tags:

Powered by Mephisto, Valid XHTML 1.1, Valid CSS - Supported by Wave Factory