目标C – 如何将度分秒转换为双精度

我的实际问题是 – 如何获得一个CLLocation对象时,在地图上的坐标值是可用的度分钟秒forms(作为一个string),而不是双。

所以,我正在寻找一种方法来转换Degree-Minute-Second到Double,我可以使用它来形成一个CLLocation对象。

我想出了一个更清晰的答案

// 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]; 

魔术

 - (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; } 

请注意,我只是在威斯康星州的北部和西部testing了这个坐标。 我正在使用这个工具来validation我的计算。

假设你有一个String中的坐标值 –

拆分string要在单独的string中获取Degree-Minute-Second值。

  • NSString *longlat= @"+39° 44' 39.28", -104° 50' 5.86" " (find一种逃避"串”的方法)

     //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; 

做转换的方法

 -(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; }