如何将参数与输出分开

时间:2016-05-26 10:33:09

标签: ios objective-c

我创建了一个简单的项目,其中我想要用户位置(纬度和经度)。我做得很成功。我喜欢它;

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 我想将纬度和经度分别发送到服务器, 现在我的问题是我应该如何从上面的输出中分离出lat-long。意思是我想在一个字符串中分隔+37.33370957-122.06381723这是另一个字符串......我怎么能这样做? 请为此问题提出任何解决方案......!

2 个答案:

答案 0 :(得分:0)

试试吧 1 + 37.33370957,-122.06381723&GT; +/- 5.00m(速度34.53 mps /航向294.26)@ 5/26/16,3:38:54 PM印度标准时间

  1. 将其放在字符串变量
  2. 用&#34;&gt;&#34;分割字符串字符并将第一个拆分字符串值放在另一个字符串变量
  3. 现在将它拆分为&#34;,&#34;你得到的字符分割字符串然后使用替换字符串方法&#34;&lt;&#34;人物
  4. 现在你可以得到lang&amp;很多发送它

答案 1 :(得分: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];

或许甚至可以用正则表达式更好的方法,但遗憾的是我不擅长它。