2016-05-26 3 views
0

私はユーザーの場所(緯度と経度)が必要な単純なプロジェクトを作成しています。成功しました。出力からパラメータを切り離す方法

self.locations = [[NSMutableArray alloc] init]; 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.distanceFilter = 10; 

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
self.locationManager.delegate = self; 
[self.locationManager requestAlwaysAuthorization]; 

[self.locationManager startUpdatingLocation]; 

...

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation 
{ 



// Add another annotation to the map. 
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; 
annotation.coordinate = newLocation.coordinate; 
[self.map addAnnotation:annotation]; 

// Also add to our map so we can remove old values later 
[self.locations addObject:annotation]; 

// Remove values if the array is too big 
while (self.locations.count > 1) 
{ 
    annotation = [self.locations objectAtIndex:0]; 
    [self.locations removeObjectAtIndex:0]; 

    // Also remove from the map 
    [self.map removeAnnotation:annotation]; 
} 


if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive) 
{ 
    // determine the region the points span so we can update our map's zoom. 
    double maxLat = -91; 
    double minLat = 91; 
    double maxLon = -181; 
    double minLon = 181; 

    for (MKPointAnnotation *annotation in self.locations) 
    { 
     CLLocationCoordinate2D coordinate = annotation.coordinate; 

     if (coordinate.latitude > maxLat) 
      maxLat = coordinate.latitude; 
     if (coordinate.latitude < minLat) 
      minLat = coordinate.latitude; 

     if (coordinate.longitude > maxLon) 
      maxLon = coordinate.longitude; 
     if (coordinate.longitude < minLon) 
      minLon = coordinate.longitude; 
    } 

    MKCoordinateRegion region; 
    region.span.latitudeDelta = (maxLat + 90) - (minLat + 90); 
    region.span.longitudeDelta = (maxLon + 180) - (minLon + 180); 

    _latitude1=[NSString stringWithFormat:@"Latitude:%f",newLocation.coordinate.latitude]; 
    _longitude1=[NSString stringWithFormat:@"Longitude:%f",newLocation.coordinate.longitude]; 


    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    appDelegate.fetchtokenString1 = [NSString stringWithString:_latitude1]; 
    appDelegate.fetchedriveridString1 = [NSString stringWithString:_longitude1]; 





    NSLog(@"%@",_latitude1); 
    NSLog(@"%@",_longitude1); 


    // the center point is the average of the max and mins 
    region.center.latitude = minLat + region.span.latitudeDelta/2; 
    region.center.longitude = minLon + region.span.longitudeDelta/2; 

    // Set the region of the map. 
    [self.map setRegion:region animated:YES]; 
} 
else 
{ 
    NSLog(@"App is backgrounded. New location is %@", newLocation); 
} 

私は、私は、サーバーに個別に緯度と経度を送りたい、この<+37.33370957,-122.06381723> +/- 5.00m (speed 34.53 mps/course 294.26) @ 5/26/16, 3:38:54 PM India Standard Time のような出力を得て てる今、私の質問は、私は緯度ことを分離する方法です上の出力から長時間。私は+37.33370957を1つの文字列で区切りたいとし、-122.06381723はこれが別の文字列であることを意味します...どうすればこのようにすることができますか? この問題の解決策をご提案ください...!

+0

あなたは、出力を生成するために使用するコードと、文字列としてlat&longを取得しようとしたコードを表示する必要がある人に役立ちます。コードで使用する変数の宣言を含めるようにしてください。型がどのようなものかは、ソリューションに影響します。あなたはこの情報を追加するためにあなたの質問を編集することができます** **コメントに追加しないでください。 – CRD

+0

私の質問を更新します... @ CRD –

答えて

0

これは動作するはずです:

NSString *rawString = @"<+37.33370957,-122.06381723> +/- 5.00m (speed 34.53 mps/course 294.26) @ 5/26/16, 3:38:54 PM India Standard Time"; 
NSRange range1 = [rawString rangeOfString:@"<"]; 
NSRange range2 = [rawString rangeOfString:@">"]; 
NSString *string = [rawString substringWithRange:NSMakeRange(range1.location + range1.length, range2.location - range1.location - range1.length)]; 
NSArray *components = [string componentsSeparatedByString:@","]; 
NSString *latitude = [components objectAtIndex:0]; 
NSString *longitude = [components objectAtIndex:1]; 

別の方法を:

NSString *rawString = @"<+37.33370957,-122.06381723> +/- 5.00m (speed 34.53 mps/course 294.26) @ 5/26/16, 3:38:54 PM India Standard Time"; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=\\<)(.*?)(?=\\>)" options:NSRegularExpressionCaseInsensitive error:nil]; 
NSTextCheckingResult *match = [regex firstMatchInString:rawString options:0 range:NSMakeRange(0, [rawString length])]; 
NSArray *components = [[rawString substringWithRange:[match rangeAtIndex:0]] componentsSeparatedByString:@","]; 
NSString *latitude = [components objectAtIndex:0]; 
NSString *longitude = [components objectAtIndex:1]; 

は、おそらく正規表現とのより良い方法がありますが、悲しいことに、私はそれでいいじゃありません。

+0

ありがとうalot bro ..それは完璧に動作します.. @ムラート –

+0

私は助けることができてうれしい –

0

てみ37.33370957 + < として、-122.06381723> +/- 5.00メートル(速度34.53 MPS /もちろん294.26)5/26/16、3時38分54秒PMインド標準時

  1. プット@それは文字列変数
  2. ">"文字で分割文字列を別の文字列変数に入れます
  3. "、"文字で分割して文字列を分割し、 "<"文字
  4. 今すぐ入手できますLANG &多くは、送信するためには、
関連する問題