How to seperate Latitude and Longitude Values From

2019-07-09 08:53发布

I am trying two draw route between two location,For that I Fetch all the points from Google Map API Web Service.(JSON Output Format). After parsing JSON Data and De cording points i stored all points on NSMutableArray. Each array of index contains this type of values.

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

Now i want to separate latitude and longitude values.

latitude  : +10.90180969
longitude : +76.19167328

How to get this values from each index of array?

3条回答
贪生不怕死
2楼-- · 2019-07-09 09:28

Here is something in a very crude form and I guess its a brute solution

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

Looking at the string it shows that you are printing the description of a CLLocation object "someObject" , You can also access the latitude and longitude such as

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

OutPut will be : Latitude: 28.621873 Longitude: 77.388897

but this won't give you the signs i.e "+" , or "-"

Hope it helps.

查看更多
Rolldiameter
3楼-- · 2019-07-09 09:28

This is just one way to do this.:

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
查看更多
Root(大扎)
4楼-- · 2019-07-09 09:46

This is how I do it in my code - tried to adapt to your case:

Header file:

@interface CLLocation (String)

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

@end

And the implementation:

@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];
    }
}
查看更多
登录 后发表回答