问题
My actual problem is - How to obtain a CLLocation Object when the coordinate value on the map is available in Degree-Minute-Second form (as a String), instead of Double.
So, I am Looking for a way to convert Degree-Minute-Second to Double, which i can use to form a CLLocation object.
回答1:
I came up with a bit of a cleaner answer when figuring this out
// split the string to deal with lat and lon separately
NSArray *parts = [dmsString componentsSeparatedByString:@","];
NSString *latStr = parts[0];
NSString *lonStr = parts[1];
// convert each string
double lat = [self degreesStringToDecimal:latStr];
double lon = [self degreesStringToDecimal:lonStr];
// init your location object
CLLocation *loc = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
The magic
- (double)degreesStringToDecimal:(NSString*)string
{
// split the string
NSArray *splitDegs = [string componentsSeparatedByString:@"\u00B0"]; // unicode for degree symbol
NSArray *splitMins = [splitDegs[1] componentsSeparatedByString:@"'"];
NSArray *splitSecs = [splitMins[1] componentsSeparatedByString:@"\""];
// get each segment of the dms string
NSString *degreesString = splitDegs[0];
NSString *minutesString = splitMins[0];
NSString *secondsString = splitSecs[0];
NSString *direction = splitSecs[1];
// convert degrees
double degrees = [degreesString doubleValue];
// convert minutes
double minutes = [minutesString doubleValue] / 60; // 60 degrees in a minute
// convert seconds
double seconds = [secondsString doubleValue] / 3600; // 60 seconds in a minute, or 3600 in a degree
// add them all together
double decimal = degrees + minutes + seconds;
// determine if this is negative. south and west would be negative values
if ([direction.uppercaseString isEqualToString:@"W"] || [direction.uppercaseString isEqualToString:@"S"])
{
decimal = -decimal;
}
return decimal;
}
Note that I've only tested this with coordinates in Wisconsin, which is North and West. I'm using this tool to verify my calculations.
回答2:
Let's say you have a the coordinate value in a String -
Split the String To obtain Degree-Minute-Second values in separate strings.
NSString *longlat= @"+39° 44' 39.28", -104° 50' 5.86" "
(find a way to escape the"
in the string)//separate lat and long NSArray *splitLonglat = [longlat componentsSeparatedByString:@","]; //separate Degree-Minute-Seconds NSArray *arrayLat = [[splitLonglat objectAtIndex:0] componentsSeparatedByString:@" "]; double latitude,longitude; if([arrayLat count]==3){ //get the double value for latitude latitude= [self convertDMSToDD_deg:(NSString *)[arrayLat objectAtIndex:0]//degree min:(NSString *)[arrayLat objectAtIndex:1]//minute sec:(NSString *)[arrayLat objectAtIndex:2]//seconds ]; }else{ //some values could be in decimal form in the String already, instead of Degree-Minute-Second form and we might not need to convert them. NSLog(@"latitude in decimal for %@",locationModelObject.name); latitude=[[splitLonglat objectAtIndex:0]doubleValue]; } NSArray *arrayLong= [[splitLonglat objectAtIndex:1] componentsSeparatedByString:@" "]; if([arrayLong count]==4){ //get the double value for longitude longitude= [self convertDMSToDD_deg:(NSString *)[arrayLong objectAtIndex:1]//degree min:(NSString *)[arrayLong objectAtIndex:2]//minute sec:(NSString *)[arrayLong objectAtIndex:3]//seconds ]; }else{ //some values could be in decimal form in the String already, instead of Degree-Minute-Second form and we might not need to convert them. NSLog(@"longitude in decimal for %@",locationModelObject.name); longitude=[[splitLonglat objectAtIndex:1]doubleValue]; } //add latitude longitude to the model object locationModelObject.latitude=latitude; locationModelObject.longitude=longitude;
The Method which does the conversion
-(double) convertDMSToDD_deg:(NSString*)degrees min:(NSString* )minutes sec:(NSString*)seconds {
int latsign=1;
double degree=[degrees doubleValue];
double minute=[minutes doubleValue];
double second=[seconds doubleValue];
if (degree<0){
latsign = -1;
}
else{
latsign=1;
}
double dd = (degree + (latsign* (minute/60.)) + (latsign* (second/3600.) ) ) ;
return dd;
}
来源:https://stackoverflow.com/questions/13523424/objective-c-how-to-convert-degree-minute-second-to-double