Distance to a location while user is in motion

谁说我不能喝 提交于 2019-11-28 05:17:44

问题


I'm in the process of writing an application that shows the user's distance from a fixed point as the user walks around (i.e. the label showing the distance from the user to the point is updated every time the user moves). I use a CLLocationManager with the code shown below:

- (void)viewDidLoad
{
    locationManager=[[CLLocationManager alloc]init]; 
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager startUpdatingLocation];      
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation      *)newLocation fromLocation:(CLLocation *)oldLocation 
{   
    CLLocationDistance meters = [newLocation distanceFromLocation:fixedPoint];
    self.distanceLabel.text = [[NSString alloc] initWithFormat:@"Distance: %.1f feet", meters*3.2808399];
}

The label that is supposed to show the distance from the user to the point isn't updated constantly and when it is updated, it doesn't usually show the correct distance from the user to the fixed point. I was wondering if there is a better way for me to try and do this, or do the fundamental limitations of the core location framework make this impossible. Any help will be greatly appreciated.


回答1:


Are you filtering out old (cached) positions? You should also filter based on accuracy, you probably don't want low accuracy locations.

You won't get continous or periodic update, the callback only occurs when the location has changed.

Assuming the device has GPS and can see enough GPS satellites to get a good position, this works fine.

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

    NSTimeInterval age = -[newLocation.timestamp timeIntervalSinceNow]; 

    if (age > 120) return;    // ignore old (cached) updates

    if (newLocation.horizontalAccuracy < 0) return;   // ignore invalid udpates

    ...
}


来源:https://stackoverflow.com/questions/8247820/distance-to-a-location-while-user-is-in-motion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!