Skip to content

Instantly share code, notes, and snippets.

@mmorey
Created February 1, 2013 03:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mmorey/4688909 to your computer and use it in GitHub Desktop.
Save mmorey/4688909 to your computer and use it in GitHub Desktop.
Converts NOAA string into a CLLocationCoordinate2D coordinate
- (CLLocationCoordinate2D)locationFromString:(NSString *)locationString{
CLLocationCoordinate2D coordinate;
// Extract individual components
NSMutableArray *locationColumns = [NSMutableArray arrayWithArray:
[locationString componentsSeparatedByCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]]];
// Remove empty objects
[locationColumns removeObject:@""];
// Build latitude prefix
NSString *latitudePrefix;
if(![[locationColumns objectAtIndex:1] isEqualToString:@"N"]){
latitudePrefix = @"-";
} else{
latitudePrefix = @"";
}
// Build longitude prefix
NSString *longitudePrefix;
if(![[locationColumns objectAtIndex:3] isEqualToString:@"E"]){
longitudePrefix = @"-";
} else{
longitudePrefix = @"";
}
// Create and return coordinate
coordinate.latitude = [[latitudePrefix stringByAppendingString:[locationColumns objectAtIndex:0]] doubleValue];
coordinate.longitude = [[longitudePrefix stringByAppendingString:[locationColumns objectAtIndex:2]] doubleValue];
return coordinate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment