Andrew Powell

Into The Mind of A Solutions Architect

Andrew Powell

Live GPS Visualizations With AIR & Merapi

August 23, 2008 · 8 Comments

So, my co-worked Adam Flater started up the Merapi Project a while back and asked me to contribute. Up until now, the only thing I've had time to contribute was the name. Well, after seeing the cool stuff that he and Jordan were doing at 360|Flex, I decided it was time to step up to the plate, get my shit together, and produce my own kick-ass Merapi demo.

Anyone who has been following the blogs knows that we at UM are real big into GIS and data visualization. Don't believe me? Take a look at SpatialKey to see what we can do with it.

I recorded a screencast where I walk you through the setups and the code of the application:

Java Code:

package com.universalmind.merapi;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import com.diddlebits.gpslib4j.GPS;
import com.diddlebits.gpslib4j.IDate;
import com.diddlebits.gpslib4j.IGPSlistener;
import com.diddlebits.gpslib4j.IPosition;
import com.diddlebits.gpslib4j.ITime;
import com.diddlebits.gpslib4j.Position;
import com.diddlebits.gpslib4j.PositionRadians;
import com.diddlebits.gpslib4j.Garmin.GarminGPS;
import merapi.Bridge;
import merapi.MerapiObject;
import merapi.messages.Message;
public class GPSReader extends MerapiObject implements IGPSlistener{
   GPS gps;      
   Position position = null;
   IPosition current = null;
   BufferedInputStream input;
   BufferedOutputStream output;
   
   public Bridge bridge;
   
   public static void main( String[] args )
   {
      
      new GPSReader();
   }
   
   public GPSReader()
   {
      super();
      
      bridge = Bridge.getInstance();
      
      CommPort port;
      
      try {
         port = CommPortIdentifier.getPortIdentifier("/dev/tty.USA19H1d1P1.1").open("GPSReader", 3000);
      } catch (NoSuchPortException e) {
         System.out.println("/dev/tty.USA19H1d1P1.1 not found!");
         return;
      } catch (PortInUseException e) {
         System.out.println("Port already in use by " + e.currentOwner);
         return;
      }
      
      try {
         input = new BufferedInputStream(port.getInputStream());
         output = new BufferedOutputStream(port.getOutputStream());         
      } catch (IOException e) {
         System.out.println("Error opening /dev/tty.USA19H1d1P1.1");
         return;
      }      
            
      gps = new GarminGPS(input, output);
      gps.setAutoTransmit(true);
      gps.addGPSlistener(this);
      
   }
   
public double getLat() {return __lat;}
   public void setLat(double lat) {
      this.__lat = lat;
   }
   public double getLon() {return __lon;}
   public void setLon(double lon) {
      this.__lon = lon;
   }
   public String getTime() {return __time;}
   public void setTime(String time) {
      this.__time = time;
   }
   public String getDate() {return __date;}
   public void setDate(String date) {
      this.__date = date;
   }
   
   public void dateReceived(IDate d) {
      String dateString = d.getMonth() + "/" + d.getDay() + "/" + d.getYear();
      System.out.println(dateString);
      this.setDate(dateString);
   }
   public void positionReceived(IPosition pos) {
      current = pos;
      this.setLat(toDecimalDegrees(current.getLatitude()));
      this.setLon(toDecimalDegrees(current.getLongitude()));
      Message message = new Message();
      GPSPoint point = new GPSPoint();
      point.lat = this.getLat();
      point.lon = this.getLon();
      point.date = this.getDate();
      point.time = this.getTime();
      message.setData(point);
      try {
         bridge.sendMessage(message);
      } catch (Exception e) {
         
         System.out.println(e.getMessage());
      }
      
   }
   public void timeReceived(ITime t) {
      String timeString = t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds();
      this.setTime(timeString);
      System.out.println(timeString);
   }
   
   private double toDecimalDegrees(PositionRadians rads){
      return Math.toDegrees(rads.getRadians());
   }
   
   private double __lat = 0;
private double __lon = 0;
private String __time = "";
private String   __date = "";
}

Tags: Merapi · MapQuest · Java · Flex · General · Adobe · Universal Mind · MapMyPix · AIR

8 responses so far ↓

  • 1 Jeff Bouley // Aug 23, 2008 at 11:31 AM

    Very cool Andy. Great job.
  • 2 Tom Link // Aug 23, 2008 at 3:08 PM

    Cool stuff, Andy!
  • 3 Devo // Aug 26, 2008 at 11:13 PM

    Hey Andy, Great code..but the QT video isn't loading for the screencast for me. Any way you can put that in flash instead?
  • 4 Andrew Powell // Sep 5, 2008 at 8:54 AM

    @Devo - I will work on it.
  • 5 Andrew Trice // Sep 14, 2008 at 1:31 PM

    Very cool. Good to see I'm not the only one that is obsessed with GPS :) <br /><br />I've actually done something very similar with AIR + Merapi using open source software for the mapping and parsing of NMEA-compliant GPS data across USB.
  • 6 Ranadheer Singh // Feb 7, 2009 at 7:58 AM

    Hi,<br /><br />i need help in merapi. i am using this in my application. some times i am getting error to start the server. error&quot;java.net.BindException: Address already in use: JVM_Bind&quot;. how to close the already opened port. Please help.<br /><br />Thank you,<br />Ranadheer.
  • 7 Andrew Powell // Feb 8, 2009 at 7:02 PM

    It would seem like you've got a rogue process running. Shut down any java processes and try to relaunch your app.
  • 8 John Gag // Oct 27, 2009 at 8:55 PM

    @Andrew - I was wondering if I could get a copy of the sample code.<br /><br />Thanks in advance

Leave a Comment