我的实际问题是 - 当地图上的坐标值以度-分-秒形式(作为字符串)而不是 Double 可用时,如何获取 CLLocation 对象。
所以,我正在寻找一种将度-分-秒转换为双精度的方法,我可以用它来形成一个 CLLocation 对象。
我的实际问题是 - 当地图上的坐标值以度-分-秒形式(作为字符串)而不是 Double 可用时,如何获取 CLLocation 对象。
所以,我正在寻找一种将度-分-秒转换为双精度的方法,我可以用它来形成一个 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;
}
请注意,我只使用威斯康星州的坐标(即北部和西部)对此进行了测试。我正在使用这个工具来验证我的计算。
假设您在字符串中有一个坐标值 -
拆分字符串以在单独的字符串中获取度-分-秒值。
NSString *longlat= @"+39° 44' 39.28", -104° 50' 5.86" "
(找到一种方法来逃避"
字符串中的)
//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;
}