-->

如何单独的纬度和经度值从JSON输出?(How to seperate Latitude and L

2019-09-16 11:55发布

我想两个定位之间两队战平的路线,对于我获取从谷歌地图API的Web服务的所有点。( JSON输出格式)。 解析后JSON数据和De盘带点我存储上的所有点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

如何从阵列中的每个指标该值?

Answer 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

但是这不会给你的迹象,即“+”或“ - ”

希望能帮助到你。



Answer 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


Answer 3:

这就是我要做的事在我的代码 - 努力去适应你的情况:

头文件:

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


文章来源: How to seperate Latitude and Longitude Values From the JSON Output?