Created
August 13, 2010 13:01
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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