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

时间:2014-03-21 11:41:34

标签: ios iphone objective-c ios7 cllocation

我正在使用一些Ploy-line Decoded Strings,我将它们存储在一个数组中。不,我想从它们中分离纬度和经度值。

我遵循这个link。它适用于一个字符串。如果我想从多个中获取Lat Long值,则会抛出异常。

这是我写的代码:

 NSMutableArray *locArr=[NSMutableArray array];

 for(NSDictionary *dict in polylineArr)
 {
     NSMutableString *str=[dict valueForKey:@"points"];
     NSArray *Arr=[self decodePolyLine:str];
     [locArr addObjectsFromArray:Arr];
 }
 NSLog(@"%@",locArr);
 for(int i=0;i<[locArr count];i++)
 {
     NSLog(@"%@",[locArr objectAtIndex:i]);

     NSMutableString * str = [[NSMutableString alloc]init];
     str = [locArr objectAtIndex:i];

     NSLog(@"%@",str);
     str = [[str componentsSeparatedByString:@">"] objectAtIndex:0];

     str = (NSMutableString *)[str substringFromIndex:1];

     NSString* strLat = (NSString*)[[str componentsSeparatedByString:@","] objectAtIndex:0];
     NSString* strLon = (NSString*)[[str componentsSeparatedByString:@","] objectAtIndex:1];
     NSLog(@"%@",strLat);
     NSLog(@"%@",strLon);
}

例外是:

  

应用程序[5228:70b] ***由于未捕获的异常而终止应用程序   'NSInvalidArgumentException',原因:' - [CLLocation   componentsSeparatedByString:]:发送到实例的无法识别的选择器   0x1309edd0'

控制台输出是:

 "<+16.30270004,+80.43871307> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30273056,+80.43869019> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30310059,+80.43843842> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30349922,+80.43820190> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30388069,+80.43795776> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30422020,+80.43772888> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30462074,+80.43750763> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30500031,+80.43731689> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30522919,+80.43720245> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30540085,+80.43711090> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30554008,+80.43702698> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30578041,+80.43691254> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30610085,+80.43673706> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30616951,+80.43669891> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30632019,+80.43662262> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30648041,+80.43653107> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30648041,+80.43653107> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30648994,+80.43652344> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30648994,+80.43650818> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30648994,+80.43650055> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30649948,+80.43649292> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30649948,+80.43647766> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30651093,+80.43647766> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30651093,+80.43647003> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30652046,+80.43647003> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30652046,+80.43646240> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30653000,+80.43646240> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30653000,+80.43644714> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30653954,+80.43644714> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30654907,+80.43643951> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30656052,+80.43643951> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30657005,+80.43643951> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30657005,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30657959,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30658913,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30660057,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30661011,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30661964,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30662918,+80.43643951> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30664062,+80.43643951> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30665016,+80.43644714> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30665970,+80.43644714> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30666924,+80.43646240> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30668068,+80.43647003> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30669022,+80.43647766> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30669022,+80.43649292> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30669975,+80.43649292> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30669975,+80.43650055> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30669975,+80.43650818> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30670929,+80.43652344> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time"
 )
 2014-03-21 16:56:01.952 Application[5228:70b] <+16.50616074,+80.64797211> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time
 2014-03-21 16:56:04.937 Application[5228:70b] <+16.50616074,+80.64797211> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time
 2014-03-21 16:56:06.104 Application[5228:70b] -[CLLocation componentsSeparatedByString:]: unrecognized selector sent to instance 0x1309edd0
 2014-03-21 16:56:06.108 Application[5228:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocation componentsSeparatedByString:]: unrecognized selector sent to instance 0x1309edd0'
 *** First throw call stack:

 libc++abi.dylib: terminating with uncaught exception of type NSException

4 个答案:

答案 0 :(得分:3)

错误实际上是在您看到的错误消息中诊断出来的:

-[CLLocation componentsSeparatedByString:]: unrecognized selector sent to instance 0x1309edd0

这告诉您,您已将-componentsSeparatedByString:发送到CLLocation个对象,当然,CLLocation没有回复该消息。你不会说出异常发生在哪一行,但我猜它就是这一行:

str = [locArr objectAtIndex:i];

locArray包含CLLocation个对象,而不是NSString个对象。


还有几点意见:

导致错误的行上方的行

NSMutableString * str = [[NSMutableString alloc]init];

是多余的。它分配一个新的NSMutableString并将str设置为指向它的指针。但是下一行会立即覆盖str

最后,你的for循环可以用快速迭代代替:

for (CLLocation* location in locArray)
{
    // All the stuff inside
}

答案 1 :(得分:0)

您正在将componentsSeparatedByString消息发送到CLLocation类型的变量,即使您认为它是NSString。我建议稍微嵌入你的代码。而不是做这样的事情:

NSString* strLat = (NSString*)[[str componentsSeparatedByString:@","] objectAtIndex:0];

将其分成多行,您实际将每个要提取的内容分配给正确类型的变量。这将有助于发现(并防止)这样的错误。

希望这有帮助。

答案 2 :(得分:0)

这看起来很可疑。

str = [locArr objectAtIndex:i];
NSLog(@"%@",str);
str = [[str componentsSeparatedByString:@">"] objectAtIndex:0];

您正在尝试在CLLocation对象中调用字符串方法。 locArr包含NSString或CLLocation项?

答案 3 :(得分: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];
    }
}