Web上的Apple和others建议您使用某些版本的委托方法didUpdateLocations
通过Core Location获得速度:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *updatelocation = locations.lastObject;
double speed = updatelocation.speed;
speed = speed * 1.94384; //converting speed from m/s to knots(unit)
NSLog(@"Speed %f",speed);
}
但是,为了省电,Apple also recommends在didUpdate方法中调用stopUpdatingLocations:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Get a fix on the user's location
...
// Stop location updates
self.locationManager.stopUpdatingLocation()
}
我发现当我调用stopUpdatingLocations时,速度经常卡在零上,因此它始终显示为零。如果删除stopUpdating
行,则速度正常。似乎需要多个观察才能获得准确的速度,或者至少立即调用该方法中的stopUpdatingLocations
会阻止获得可靠的速度读数。
应用程序不一定总是知道速度。当用户要求时,它应该提供准确的读数。
谁能建议一种可行的方法,以可接受的用电量水平获得准确的速度读数?