image
www.flickr.com
William C. Arter's photos More of William C. Arter's photos

« Web Readings for 1/26 | Main | iPhone version of the location/weather example »

January 26, 2009

Location Tutorial | Mobile Processing @ 8:34 PM

As we went through in class today, it's pretty easy to get your phone's location using Mobile Processing and the GPS on the N95.

First, you'll want to grab the mlocation library from Mary Jane Soft HERE.

Once the zip file is downloaded, expand it so you have an mlocation folder. Drag that mlocation folder into your mobile processing application folder:

YOUR_MOBILE_PROCESSING_DIRECTORY/libraries/

You should see some other libraries already there. Once you do that, restart mobile processing if it's already open. Otherwise, just open it and you're good to go.

You can download the .pde (mobile processing sketch) here:

Download file

The main parts of code you should be interested in are...

This bit connects to the server nodesnoop.com:


client = new PClient(this, "www.nodesnoop.com");

After that connection has been made, we're going to query the mlocation library for the latitude and longitude.


MLocation.location(coords);

This sticks the latitude and longitude in the coods array, which we will use when we send the request to the server we just connected to:


request = client.GET("/clients/405/weather.php?lat=" + coords[0] + "&lon=" + coords[1]);

Here we're sending an http get request to the server we connected to above. We're requesting the file weather.php, and we're sending that file the parameters lat and lon, which are assigned values based on the coords variable we get from MLocation.

in the libraryEvent callback function we'll look for the file to be completed


} else if (event == PRequest.EVENT_DONE) {
//done reading
msg = new String((byte[]) data);
println(msg); //print out in the mobile processing debug window for testing purposes
request.close(); //close the request
redraw(); //call the draw() function once
}

Basically, this just says: "When the file is loaded completely, set the msg variable to the output of the file we requested." In this case, the file returns the current temperature of the latitude and longitude it was given.

After we do this, we'll call redraw() which calls the draw() method once.

First, we'll check if the msg variable got set correctly. If there was some issue, it will not be set, and therefore be null.


if(msg == null) {
background(255);
}

Otherwise, we'll do something based on that temperature value. In this instance, we're just changing the background color of the app based on the temp., but I'd imagine you can come up with something better than that.


} else {
//if we do, do something with that information.
//in this example, we compare the integer value of msg (the integer temperature)
//with a range of values to change the background color

//YOU CAN EDIT THIS CODE TO MAYBE DO SOMETHING MORE INTERESTING

if(int(msg) > 60 && int(msg) <= 70) {
background(255,255,0);
} else if(int(msg) > 70 && int(msg) <= 100) {
background(255,0,0);
} else {
background(0,0,255);
}
}

| all rights william carter |
| view cc license |

Thanks for signing in, . Now you can comment. (sign out)


Remember me?