0

我曾在一个应用程序名称时间跟踪器上工作过。用户可以通过单击按钮手动滑入和手动滑出。

现在我想根据位置检测将其设为自动。为此,我正在使用CLLocationManager类。它有时工作正常,有时会给出错误的滑动​​细节。我正在使用下面的代码。

- (void)viewDidLoad {
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     locationManager = [[CLLocationManager alloc] init];
     locationManager.delegate = self;
     locationManager.distanceFilter = kCLDistanceFilterNone;
     locationManager.desiredAccuracy = kCLLocationAccuracyBest;

     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
         [locationManager requestWhenInUseAuthorization];

      [locationManager startUpdatingLocation];

}

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

     _latitude.text = [NSString stringWithFormat:@"Latitude: %f", newLocation.coordinate.latitude];
     _longitude.text = [NSString stringWithFormat:@"Longitude: %f", newLocation.coordinate.longitude];

     if([_latitude.text doubleValue] > 17.76890) &&  [_longitude.text doubleValue] > 78.34567) {

         if (isSwipeIn) {
             isSwipeIn = false;
             //necessary swipe out UI and logic
         } else {
             isSwipeIn = true;
             //necessary swipe in UI and logic
      }
   }
}

谁可以帮我这个事..

4

2 回答 2

1

与其比较经纬度,不如进行范围检查,例如您的设备是否在几米标记之内,因为刷入否则刷出。

您可以在 Objective-C 中使用以下方法检查两个 lat-long 之间的距离

CLLocation *location; // Your Location to compare
CLLocation *currentLocation; // Your current location
double distance = [location distanceFromLocation:currentLocation]; // Returns distance in meters

// Now lets say you are within 5 meters mark Swipe In

if(distance <= 5)
     // Mark swipe IN
else 
     // Mark swipe OUT

我希望这能帮到您。快乐编码:)

于 2016-10-04T10:21:26.580 回答
0

还有另一种方法可以做到这一点,您可以获取与目标位置的距离并与horizontalAccuracy当前位置进行检查。

为您提供当前位置和目标位置之间的delta距离。如果delta小于 (<) horizontalAccuracy,则当前位置在半径为 的圆中horizontalAccuracy

如果delta大于 (>) horizontalAccuracy,则当前位置比您的目标位置远。

所以现在CLLocationManager委托方法将如下所示:

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

    _latitude.text = [NSString stringWithFormat:@"Latitude: %f", newLocation.coordinate.latitude];
    _longitude.text = [NSString stringWithFormat:@"Longitude: %f", newLocation.coordinate.longitude];

    // Create Location object for your target location. e.g. (17.76890,78.34567)
    CLLocation *targetLocation = [[CLLocation alloc] initWithLatitude:17.76890 longitude:78.34567];
    CLLocationDistance delta = [newLocation distanceFromLocation:targetLocation];

    if (delta > newLocation.horizontalAccuracy) {
        if (isSwipeIn) {
            isSwipeIn = false;
            //necessary swipe out UI and logic
        } else {
            isSwipeIn = true;
            //necessary swipe in UI and logic
        }
    }
}
于 2016-10-04T10:19:12.143 回答