Monday, October 12, 2009

Who wrote this crap?

It never ceases to amaze me how code that is written 2 or 3 months ago almost always ends up being crap.  I am exaggerating of course, but a lot of time the code I write when I revisit later is just crazy. 

I'm sure part of that is because the code I write on my own is often done at 2 or 3 in the morning.  It is interesting though, I can clearly see where the coding sessions were broken up instead of flowing smoothly in one session.


So, CoreLocation is one of the frameworks in the iPhone SDK.  I used it quite a bit with the LifeAware iPhone app and am using it again for my current project.  In both project I ran into the cached location issue.  Now if you read the SDK and even look at the sample code for this framwork you will see things like this:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [locationMeasurements addObject:newLocation];
    // test the age of the location measurement to determine i
f the measurement is cached
    // in most cases you will not want to rely on cached measurements
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;

This code does not seem to work.  I tried so many variations and it just doesn't work.  I'm sure it worked for Apple and maybe it works for you.  If so that's great, good for you.  But for me it didn't work so here is how I did it.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"Found a new location %@",  newLocation);
    NSDate *now = [[NSDate alloc] init];
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"HH:mm EEEE MMMM d"];
    NSString *nowTimeString = [outputFormatter stringFromDate:now];
    NSString *locationTimeString = [outputFormatter stringFromDate:newLocation.timestamp];
    NSLog(@"The age of the location is: %@", nowTimeString);
    NSLog(@"The age of the location is: %@", locationTimeString);
    if ([nowTimeString compare:locationTimeString] == NSOrderedSame){

Basically I take the timestamp from the location object and create a new one for "now".  Convert the two to string representations and compare the strings for equality.  This gives me a location that is less than a minute old. It seems just a little bit hackish but I think it's a pretty decent solution to a flaw in the API, seriously why wouldn't you say something like:

[locationManager startUpdatingLocation useCached:NO];

Happy coding!

-Aaron

Sunday, October 11, 2009

Been a while...

So it's been a while since I last posted a blog entry.  A whole lot of things have been going on.  First of all I finished my entry to the Android Developer Challenge 2 it is a game called Dungeon Trainer

Dungeon Trainer is a simple RPG game that lets you create and train characters by battling other characters.  Once your character is created you can upload them to share with players around the world.  You level up, buy weapons and armor and then battle (train).  It is a simple game for a couple of reasons

  1. The time constraint of the challenge was very tight I started designing on June 16th and had to develop it all from concept to final product by Austst 28th.  Ruffly 2.5 months.
  2. Mobile games should be simple.  Having a game like Baler's Gate or Diablo on your phone would be cool but it would be hard to pick up and put down quickly.  Or casually play in a meeting that you were bored at.
  3. It was the first game I ever wrote so I wanted to start simple and build on it.

As of October 6th the first round of judging is complete.  I haven't heard any results yet, I'll let you know when the winners are announced though.

I'm also working on another iPhone app.  This one is pretty cool and much simpler than the LifeAware iPhone app.  I can't say much about it except that I am still doing iPhone stuff.

It is kind of a cool contrast between the 2 platforms though(android an iPhone).  My day job is Java so the Android platform was a super easy transition.  The iPhone was not easy in any way, even still I kind of enjoy developing for it.  It kind of reminds me of VB in a way although you have all the power of C to hang yourself...

I'll put some little code snippets of the iPhone out. I"m sure if I have struggled with these parts other people have as well. 

Until then ciao'.

-Aaron