2012-04-12 15 views
2

私は2つの場所の間に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

+1

ちょうどいくつかの弦操作人をしてください。 – tipycalFlow

+0

私はNSSstring、NSString "<+10.90180969、+76.19167328> +/- 0.00m(速度-1.00 mps /コース-1.00)@ 12/04/12 10:18:10 AMインドスタンダード" 次に、+10.90180969と+76.19167328の値を分離する方法 – Musthafa

答えて

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

+0

Musthafa、あなたはいつも開発者のドキュメントと開発者ポータルdeveloper.apple.comで利用可能なサンプルコードを参照することができ、それは常にStackOverflowと共に役立ちます。お力になれて、嬉しいです –

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

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]; 
    } 
} 
関連する問題