3

私の実際の問題は、マップ上の座標値がDoubleではなく、度分秒形式(文字列として)で利用可能な場合にCLLocationオブジェクトを取得する方法です。

だから、CLLocationオブジェクトを形成するために使用できるDoubleにDegree-Minute-Secondを変換する方法を探しています。

4

2 に答える 2

2

これを理解するとき、私は少しきれいな答えを思いついた

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

北と西であるウィスコンシンの座標でのみこれをテストしたことに注意してください。このツールを使用して計算を検証しています。

于 2015-02-16T15:52:03.760 に答える
1

文字列に座標値があるとしましょう -

文字列の分割 度分秒の値を別々の文字列で取得するには。

  • 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;
}
于 2012-11-23T05:09:23.270 に答える