[英]How to calculate distance between locations when one is changing user location and second one should be constant?
I have IBAction
: 我有
IBAction
:
-(IBAction)pressStart{
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
In 在
- (void)viewDidLoad
{
[super viewDidLoad];
duration.text = @"00:00:00";
speedLabel.text = @"00";
locationManager = [[CLLocationManager alloc]init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
}
And this method: 而这种方法:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *firstLocation = [locations objectAtIndex:0];
location = [locations lastObject];
CLLocationDistance meters = [location distanceFromLocation:firstLocation];
NSLog(@"meters= %f", meters);
And I don't know why the firstLocation
is changing. 而且我不知道为什么
firstLocation
正在更改。 Maybe there is a way to capture firstLocation? 也许有一种方法可以捕获firstLocation? This should be the location of the device when the button Start is pressed.
当按下开始按钮时,这应该是设备的位置。
firstLocation
in your code is not the first location since location updates started; 自位置更新开始以来,代码中的
firstLocation
并不是第一个位置; it is the first location to be returned to you since the previous callback to the locationManager:didUpdateLocations:
method (the location services may collect multiple locations before calling back to your delegate method in certain circumstances -- the most recent location is always going to be the last object in the locations array). 这是自上次对
locationManager:didUpdateLocations:
方法的回调以来返回给您的第一个位置(在某些情况下,位置服务可能会在调用您的委托方法之前收集多个位置-最近的位置始终是位置数组中的最后一个对象)。
If you need to store the first location since location updates started, you should create a property such as 如果自位置更新开始以来需要存储第一个位置,则应创建一个属性,例如
@property (nonatomic, strong) CLLocation *startingLocation;
Then in the locationManager:didUpdateLocations:
method, add the code: 然后在
locationManager:didUpdateLocations:
方法中,添加代码:
if (!self.startingLocation) {
self.startingLocation = [locations objectAtIndex:0];
}
That will store the starting location into the property after the first callback. 它将在第一个回调之后将起始位置存储到属性中。 (You can set the property to nil if you want to reset it.)
(如果要重置属性,可以将属性设置为nil。)
Don't forget that the very first location you receive many not be very accurate, as it takes time for location services to get a fix on the device's position if they were not recently enabled. 不要忘记,您收到的第一个位置不是很准确,因为如果最近未启用位置服务,则位置服务需要花费一些时间来修复设备的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.