简体   繁体   English

当一个位置更改用户位置而第二个位置更改用户位置时,如何计算位置之间的距离?

[英]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.

相关问题 计算位置与位置列表之间的距离 - calculate distance between a location and a list of locations 如何计算当前位置与JSON文件中填充的位置之间的距离 - How to calculate the distance between my current location and locations populated in a JSON File 如何计算用户移动时从用户位置到注释的距离 - How to calculate distance from user location to annotation when user moves 计算用户位置与固定位置之间的距离 - Calculate distance between user's location and a fixed location 如何使用谷歌计算两个位置之间的距离 api in swift 4 - How to calculate distance between two locations using google api in swift 4 如何计算用户当前位置和多个地理坐标之间的最短距离 - How to calculate shortest distance between user current location and multiple geo coordinate 当我的接送位置相同时,如何计算驾驶室所覆盖的点之间的总距离? - How to calculate total distance between points covered by a Cab when my pickup and drop location are same? 如何计算我当前位置和MapView中其他Pins之间的距离 - How to calculate the distance between my current location and other Pins in MapView 如何计算两个位置的距离? - How to calculate two location distance? 根据道路计算两个位置之间的距离 - Calculate distance between two location based on roads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM