2012-04-12 101 views
2

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

"<+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 

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

+0

什么样的反应是那?你可以给我看整串吗? – mChopsey 2012-04-12 05:16:56

+1

只要做一些字符串操作的人。 – tipycalFlow 2012-04-12 05:18:26

+0

假设我有一个NSString,NSString的值是“<+10.90180969,+76.19167328> +/- 0.00m(速度-1.00 mps/course -1.00)@ 12/04/12 10:18:10 AM印度标准时间” , 那么如何分离+10.90180969和+76.19167328的值 – Musthafa 2012-04-12 05:22:13

回答

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); 

输出将是:纬度:28.621873经度:77.388897

但是这不会给你的标志即“+”或“ - ”

希望它能帮助。

+0

谢谢...,我选择第二种方法。 – Musthafa 2012-04-12 06:28:40

+0

Musthafa,您随时可以参考开发人员文档和开发人员门户网站developer.apple.com上提供的示例代码,这与StackOverflow一起很有帮助:)。乐于帮助 – 2012-04-12 07:00:23

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 
+0

我选择上面的答案第二个 – Musthafa 2012-04-12 06:32:41

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