2

我正在尝试两个位置之间的两条绘制路线,为此我从 Google Map API Web Service 获取所有点。(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

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

4

3 回答 3

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
于 2012-04-12T05:48:38.410 回答
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

但这不会给你符号,即“+”或“-”

希望能帮助到你。

于 2012-04-12T05:50:01.233 回答
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];
    }
}
于 2014-04-04T23:29:01.063 回答