iOS位置转换

时间:2013-11-20 02:50:39

标签: ios google-maps

我正在创建一个利用位置的应用程序,我注意到所使用的纬度和经度值的形式为:21.232 -54.234。现在,当我在Google Maps上找到特定位置时,Lat和Long值显示为:23°4.800', - 34°30.829'。 (这些只是组成值,没有具体的)。我想从谷歌地图中获取位置并将它们放入我的应用程序中。这两种形式之间有转换吗?

2 个答案:

答案 0 :(得分:2)

使用以下代码:

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

答案 1 :(得分:0)

以下是将google lat / lon字符串转换为浮点值的示例:

- (void)convertGoogleLatLonString:(NSString *)latLonString {
    latLonString = @"23° 4.800', -34° 30.829'"; // Cheat for demo
    NSLog(@"latLonString: %@", latLonString);

    NSArray *latLonArray = [latLonString componentsSeparatedByString:@","];
    float lat = [self convertGoogleLLString:latLonArray[0]];
    float lon = [self convertGoogleLLString:latLonArray[1]];

    NSLog(@"lat: %f", lat);
    NSLog(@"lon: %f", lon);
}

- (float)convertGoogleLLString:(NSString *)llString {
    NSString *pattern = @"(-?\\d+)[^0-9]+([-0-9.]+)";
    NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:pattern
                                  options:NSRegularExpressionCaseInsensitive
                                  error:nil];

    NSTextCheckingResult *tcResult = [regex firstMatchInString:llString options:0 range:NSMakeRange(0, llString.length)];
    NSString *degMatch = [llString substringWithRange:[tcResult rangeAtIndex:1]];
    float degees = [degMatch floatValue];
    NSString *minMatch = [llString substringWithRange:[tcResult rangeAtIndex:2]];
    float minutes = [minMatch floatValue];

    float latOrLon = [self decimalLatOrLonWithDegrees:degees minutes:minutes];

    return latOrLon;
}

- (float)decimalLatOrLonWithDegrees:(float)degees minutes:(float)minutes {
    minutes = (degees > 0) ? minutes : -minutes;
    float latOrLon = degees + minutes/60;

    return latOrLon;
}

NSLog输出:

  

latLonString:23°4.800', - 34°30.829'
  纬度:23.080000
  lon:-33.486183