Skip to content

Instantly share code, notes, and snippets.

@nolim1t
Created August 13, 2010 13:01
Show Gist options
  • Save nolim1t/522837 to your computer and use it in GitHub Desktop.
Save nolim1t/522837 to your computer and use it in GitHub Desktop.
Snippet to get the closest location (The dataset must be an array of dictionaries)
// 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