2012-04-12 39 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]; 
    } 
} 
相關問題