Posts Tagged ‘ google maps

Elevation for routes

I have been playing around with the GPS functionality of the iPhone for a while now and I think I have optimized the collection of in-plane GPS data as best as possible given the limitations of the hardware.  The iPhone was not created to be a garmin forerunner and this especially shines through when you try to get elevation data out of the iPhone GPS.  This became painfully obvious after I built the functionality for collecting and displaying elevation data from the GPS.  Even after creating sophisticated methods for filtering the GPS data and averaging the data and weighing the data by the vertical accuracy the graphs of the running data had no correlation with reality.  At first this was disappointing – I had created this great code for collecting, saving, and displaying the elevation data but clearly if I released an app which was going to show unrealistic elevation data – the app would get killed by bad reviews.

So I looked around for other places to get elevation data.  Turns out that the space station has used lasers to measure elevation all over the surface of the earth and that Google has an API which allows you to query that data.  This means much better resolution on the elevation data than a GPS (even a garmin) could ever provide and I can show elevation data for both current routes as well as routes that have been saved which have no elevation data saved with them.  The only issue is that a data connection is required in order to get at this data but the amount of data being sent back and forth is so small that the elevation data shows up almost instantly even on an EDGE connection.  Being a good citizen I only have the app request elevation data if the user is interested in seeing this type of data – this keeps data usage down and keeps load off Google’s servers.

The video below shows how the elevation data is displayed within the app.

  • Share/Bookmark

iPhone OS 4.0

Great news for all the running/biking/hiking iPhone applications yesterday – OS 4.0 will not only allow you to run location based apps in the background – but you can now also have much richer overlays for maps.  These updates to the  iPhone OS will make it possible to make running apps that will really take advantage of the iPhone hardware and bring great experiences to the active people using these apps.

  • Share/Bookmark

How to test location and accelerometer functions from the comfort of your home

The LogYourRun iPhone app uses both the GPS and the accelerometer functions of the iPhone.  Neither the GPS nor the accelerometer can be accessed on the iPhone simulator so to circumvent this I used the following methods:

Accelerometer: Otto Chrons has a great Google code project which contains the code for an app that can be run on an iPod touch or iPhone which is on the same subnet as your development computer.  The app will transmit accelerometer data which can be caught by some code that you can insert into your Xcode project.  When you run your app on the iPhone simulator it will thus receive the accelerometer data from the real life device.  This was a great help in troubleshooting the setup of the pedometer functionality.  I was running around in my living room counting steps and comparing to the # of steps on the iPhone simulator.  Be sure to remove the accelerometer simulator code from your application before releasing.

GPS: Everyone who has played with the location manager on the iPhone Simulator knows that you will only get only one location from the location manager on the iPhone Simulator.  Testing the GPS functionality is a critical part of testing the LogYourRun app.  I could run the app on an iPhone and then run/walk/drive around outside – but that would take much too long and the weather in Saint Louis is not conductive to such testing.  Instead I added a button to the pedometer screen of the application which when clicked runs the following code:

- (IBAction) locationButton:(id)sender {
   CLLocation *thisLocation;
   double latitude  = 37.33168900 + (float) ((random() % 100) +1) / 1000000.0;
   double longitude = -122.03073100 + (float) ((random() % 100) +1) / 1000000.0;
   thisLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
   [self locationManager:locationManager didUpdateToLocation:thisLocation fromLocation:thisLocation];
   [thisLocation release];
}

This piece of code creates a random location close to the location that the iPhone Simulator will usually get from the core location (One Infinite Loop). The location is sent to the location manager delegate function of the pedometer class and is handled as if it came from the real location manager. Since the location is different from the previous location the getDistanceFrom: still works.  When I am ready to submit the application all I have to do is hide the two buttons (probably should be done programatically – since one of these days I will forget).

Pedometer showing location and step button

The pedometer screen showing location and step button

Locations shown on map

The random locations shown on map

  • Share/Bookmark

Encoding latitude and longitude the Google way

The LogYourRun iPhone App uses the GPS functionality of the iPhone to track running distance as well as the running route.  When data is uploaded from the app to the website the list of GPS coordinates have to be transferred from the phone to the website.  Since I already store route GPS data on the LogYourRun website as an encoded Google polyline I decided to have the iPhone encode the GPS data before sending to the website.  This takes processing load off the server and allows for faster transfer of data from the iPhone to the website.

I already have a JavaScript and a PHP version of the functions that both encode and decode GPS data as a Google polyline – so it was just a matter of converting the functions to Objective-C / Cocoa.

The way that the encoder works is first it multiples the coordinate by 10e5, takes the floor value and compares it to the coordinate to the 10e5 value of the previous coordinate and encodes the difference between the two points.  The following are functions used to perform the basic math.

This function is used to multiply the coordinate by 10e5:

- (int) floor1e5: (double) coordinate {
 return (int) floor(coordinate * 1e5);
}

This function is used to encode a signed number:

- (NSString *) encodeSignedNumber: (int) num {
int sgn_num = num << 1;
 if (num < 0) {
 sgn_num = ~(sgn_num);
 }
 return [self encodeNumber: sgn_num];
}

And this function is used to encode an unsigned int:

- (NSString *) encodeNumber: (int) num {
 int nextValue;
 NSMutableString *encodeString = [[NSMutableString alloc] initWithCapacity:5];
 [encodeString autorelease];

 while (num &gt;= 0x20) {
 nextValue = (0x20 | (num &amp; 0x1f)) + 63;
 [encodeString appendFormat:@"%c", ((char) (nextValue))];
 num &gt;&gt;= 5;
 }

 num += 63;
 [encodeString appendFormat:@"%c", ((char) (num))];

 return encodeString;
}

The following function is used to encode an array of CoreLocation objects (CLLocation *).  This array is called locations in the following code.  The  first thing the function does is to make sure that there are points in our locations assay.  Next I setup a mutable string which is going to contain the encoded polyline string as well as a temporary CoreLocation object (tmploc).  Since we have not started going through the locations we set the previous latitude and longitude to 0 each (plat and plng).  Then I go through the locations array and put them into the temploc.  For each location the previous latitude and longitude is subtracted from the current after the 1e5 conversion.  The encoded latitude and longitude are then added to the encoded polyline string which is returned at the end of the function.

- (NSString *) googlePolyline {
 int i, late5, lnge5, dlat, dlng, plat, plng;

 if ([locations count] == 0) {
 return @"";
 }

 NSMutableString *encodedPoints = [[NSMutableString alloc] initWithCapacity:100];
 [encodedPoints autorelease];
 CLLocation *tmploc;

 plat = 0;
 plng = 0;

 for (i = 0; i &lt; [locations count]; i++) {
 tmploc = [locations objectAtIndex:i];

 late5 = [self floor1e5: tmploc.coordinate.latitude];
 lnge5 = [self floor1e5: tmploc.coordinate.longitude];

 dlat = late5 - plat;
 dlng = lnge5 - plng;

 plat = late5;
 plng = lnge5;

 [encodedPoints appendFormat:@"%@%@", [self encodeSignedNumber:dlat], [self encodeSignedNumber:dlng]];

 }
 return [[[NSString alloc] initWithFormat: @"%@", encodedPoints] autorelease];
}

The googlePolyline function can be used to convert an array of location objects to a simple string which can then applied directly to a google map.

For other ports of the google polyline encoder see:

http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/

  • Share/Bookmark