Skip to content

Instantly share code, notes, and snippets.

@logicaroma
Forked from nolim1t/gist:522837
Created April 8, 2011 01:32
Show Gist options
  • Save logicaroma/909133 to your computer and use it in GitHub Desktop.
Save logicaroma/909133 to your computer and use it in GitHub Desktop.
// Method: What is Nearer helper function
// Pre-requisite: dataset loaded in the app delegate
-(NSDictionary *) whatIsNearer:(CLLocation *)currentLocation {
whichway_ipadAppDelegate *appdelegate = (whichway_ipadAppDelegate *)[[UIApplication sharedApplication] delegate];
float closest_distance;
int linecnt = 1;
NSDictionary *tmpDict = [[[NSDictionary alloc] init] autorelease];
for (NSDictionary *store in appdelegate.dataset) {
NSString *latlong = [store objectForKey:@"LatLong"];
NSArray *latlongarray = [latlong componentsSeparatedByString:@","];
CLLocationDegrees lat = [[latlongarray objectAtIndex:0] doubleValue];
CLLocationDegrees longitude = [[latlongarray objectAtIndex:1] doubleValue];
CLLocation *tmploc = [[[CLLocation alloc] initWithLatitude:lat longitude:longitude] autorelease];
CLLocationDistance current_dist = [tmploc distanceFromLocation:currentLocation];
if (linecnt == 1) {
closest_distance = current_dist;
tmpDict = store;
} else {
if (current_dist < closest_distance) {
tmpDict = store;
}
}
linecnt++;
}
return tmpDict;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment