如何从JSON输出中分离纬度和经度值?

时间:2012-04-12 05:13:35

标签: iphone objective-c ios mkmapview cllocation

我正在两个位置之间尝试两条绘制路线,为此我从Google Map API Web服务获取所有点。(JSON输出格式)。在解析JSON数据和记录点后,我存储了NSMutableArray上的所有点。 每个索引数组都包含这种类型的值。

"<+10.90180969, +76.19167328> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/04/12 10:18:10 AM India Standard Time",

现在我想分开纬度和经度值。

latitude  : +10.90180969
longitude : +76.19167328

如何从数组的每个索引中获取此值?

3 个答案:

答案 0 :(得分:2)

这只是一种方法。:

NSString* str = @"<+10.90180969, +76.19167328> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/04/12 10:18:10 AM India Standard Time";//you already have this string.
str = (NSString*)[[str componentsSeparatedByString:@">"] objectAtIndex:0];
// after above performed step, str equals "<+10.90180969, +76.19167328"
str = [str substringFromIndex:1];
// after above performed step, str equals "+10.90180969, +76.19167328"
NSString* strLat = (NSString*)[[str componentsSeparatedByString:@","] objectAtIndex:0];
NSString* strLon = (NSString*)[[str componentsSeparatedByString:@","] objectAtIndex:1];
// after above performed step, strLat equals "+10.90180969"
// after above performed step, strLon equals " +76.19167328"
strLon = [strLon substringFromIndex:1];//<-- to remove the extra space at index=0

答案 1 :(得分:1)

这是一种非常粗糙的形式,我猜它是一个粗暴的解决方案

NSString* str =     @"<+10.90180969, +76.19167328> +/- 0.00m (speed -1.00 mps  course -1.00) @ 12/04/12 10:18:10 AM India Standard Time";
NSArray* arr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

if ([arr count] > 1) {
   NSString* coordinateStr =  [arr objectAtIndex:1];        
   NSArray* arrCoordinate = [coordinateStr componentsSeparatedByString:@","];        
    NSLog(@"%@",arrCoordinate);
}

查看字符串,表明您正在打印CLLocation对象“ someObject ”的描述,您还可以访问纬度和经度,例如

   CLLocationCoordinate2D location = [someObject coordinate];
    NSLog(@" \nLatitude: %.6f \nLongitude: %.6f",location.latitude,location.longitude);

OutPut将是:纬度:28.621873经度:77.388897

但这不会给你一些标志,即“+”或“ - ”

希望它有所帮助。

答案 2 :(得分:0)

这就是我在代码中执行的操作 - 尝试适应您的情况:

标题文件:

@interface CLLocation (String)

+ (instancetype) clLocationWithString:(NSString *)location;

@end

实施:

@implementation CLLocation (String)

+ (instancetype)clLocationWithString:(NSString *)location
{
    static NSRegularExpression *staticRegex;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSError *error = NULL;
        //ex: <41.081445,-81.519005> ...
    staticRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\-?\\d+\\.?\\d*)+"
                                                            options:NSRegularExpressionCaseInsensitive
                                                              error:&error];
    });

    NSArray *matches = [staticRegex matchesInString:location options:NSMatchingReportCompletion range:NSMakeRange(0, location.length)];

    if (matches.count >= 2) {
        return [[CLLocation alloc] initWithLatitude:[[location substringWithRange:((NSTextCheckingResult *)[matches objectAtIndex:0]).range] doubleValue]
                                          longitude:[[location substringWithRange:((NSTextCheckingResult *)[matches objectAtIndex:1]).range] doubleValue]];
    } else {
        return [[CLLocation alloc] init];
    }
}