Getting GPS data on your N95 using Mobile Processing is easier than you might think. Here’s how to do it.
If you read my previous article about Mobile Processing, you already know something about the application. The summary: it’s a programming environment for your Mac or PC that makes it much easier to write Java (J2ME) apps for your mobile device. In this case, I’ll use the N95 as an example.
Even if you’re new to this, you’ll find that Mobile Processing is a great way to learn programming basics. And it’s Java, so you can make powerful apps with it once you know how. It’s also free and open source, so it’s a great place to start.
I’m going to assume you have at least passing knowledge of basic programming structures and maybe some Java experience. If you are new to Mobile Processing and have not run through my previous article, please do. It’ll help you get set up - and it’ll run you through a quick test to make sure everything’s working properly.
Done that? Great. Now let’s get into creating a GPS-enabled mapping application. (If you’d like to learn more about how the Global Positioning System works, check out the GPS Wikipedia entry.)
Getting the mLocation library
What is mLocation? mLocation is going to give us awesome and incredibly easy access to the GPS device built into the N95. With just a few lines of code we’ll be able to find out exactly where on Earth we are.
First, we are going to make things easy for ourselves by downloading the mLocation library for Mobile Processing. Grab it here. (Yes, the site is in Spanish.) Download “mlocation_0.2.1.zip” and unzip it somewhere on your computer. Then copy the “mlocation” folder into the “libraries” folder inside of the Mobile Processing folder. On my computer, a Mac, the Mobile Processing folder is inside my main Applications folder and named “Mobile-0006″ where “0006″ is the version number. Inside of that is a folder called “libraries”. Put the whole “mlocation” folder in there.
Launch Mobile Processing. If you did it right, you should be able to go to the Sketch menu and look at the Import Library submenu. Do you see mlocation in there? Yes? Great! No? Then go back and make sure you put it in the right place. Also, if you installed the library while Mobile Processing is running, you’ll need to quit Mobile Processing and open it up again.
Running the code
Okay. Here’s the code we’re going to use to get the GPS data. I’m just going to give it to you and show you how to run it. Then I’ll discuss exactly what’s going on with it. Ready? Set? Go:
import mjs.processing.mobile.mlocation.*;
PFont font;
double[] coords = new double[2];
void setup() {
font = loadFont();
textFont(font);
textAlign(CENTER);
MLocation.location(coords);
}
void draw() {
background(0);
text(”Latitude : ” + coords[0], width/2, height/2-10);
text(”Longitude : ” + coords[1], width/2, height/2+10);
}
If you try to run this within Mobile Processing on your computer, you’re probably going to get an error, because mLocation can’t find a usable GPS device. So we’re going to have to deploy this onto the phone to test it out. Deploy according to my instructions in the previous Mobile Processing article (just go to the Export MIDlet option from the File menu and Bluetooth the resulting “jad” and “jar” file onto your N95. Run the jad file on the phone and it’ll perform the installation for you. The app will be found in the Applications folder, probably right at the bottom.)
If it worked properly, your N95 will ask permission to use the GPS. After a few seconds, you should see a black background with white text showing your latitude and longitude.
Note: You may have to wait up to a minute or so to get the GPS data, and you’ll want to find a clear view of the sky – judging by the bottom of the device, which is where the GPS locator is - in order to find a connection. If you’re indoors, go to a window or another place that may potentially give you that clear view. Also, be patient. If you wait for over a minute without any response, exit the application and try running it again.
How the code works
Let’s take a closer look at that code. What’s it doing?
You’ll notice that the code is broken up into four main parts. The first part is where we import everything we need - in this case, just one line for the mLocation library. If you go to the Sketch menu and look in the Import Library submenu, you’ll see all of the available libraries. More can be installed from the Mobile Processing site, as well, to handle things such as Bluetooth or more advanced graphics functions.
In the second part - lines 3 and 4 - we set up our global variables. These variables will be available to us anywhere within our application.
The third part is the “setup” function I described in my last Mobile Processing article. Everything inside “setup” runs exactly once - right when the application launches. So you can see we’re setting up the font we’ll use and then, in that last line, grabbing the GPS data from mLocation. Notice how it takes an array called “coords” and populates it with two elements, the latitude and longitude. So accessing the first element of the array - coords[0] - will get us the latitude. The second element - coords[1] - holds the longitude.
The last part is the “draw” function I also described before. This loops endlessly and redraws the screen. In this case, we’re just setting the background to be black and telling it to write out the latitude and longitude in the middle of the screen.
Final thoughts
If you’re looking for more instruction on this topic, I would recommend digging through the Mobile Processing reference. It’s quite detailed and easy to understand.
In my next article, I’m going to show you how to connect this with Yahoo!’s mapping API to make your own more sophisticated custom mapping tool. Stay tuned! And share your own ideas on this topic in the comments section.
2:06 PM
03.15.09