我已经实现了使用 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
从 gps 检索坐标的标准方法。
问题是这个函数只在初始化时被调用,而不是在程序的整个生命周期中被调用。这是正常的吗?
在 android 中,您将实现一个监听器,您将立即获取数据。
这不是我做事的正确方式吗?如果是,可能是什么问题? (顺便说一句,我检查过,我没有停止更新位置)
我有一个 CLLocationManager 继承类,名为 testing
并初始化它
testing* cllm = [[testing alloc] init];
cllm.delegate = self;
我稍后开始更新
[cllm startUpdatingLocation];
self.locationManagerDelegate = delegate;
以后称为
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
但之后它就不再被调用了。我需要经常调用它,以便我可以计算从我所在的位置到某个点 X 的距离。
最佳答案
同意@Matt,无需更多代码,我可以提供的最佳解决方案是告诉它在每次移动设备时进行更新:
[self.locationManager setDistanceFiler:kCLDistanceFilterNone]
更新
我浏览了过去的项目并找到了我相信您正在寻找的代码,假设您的位置管理器子类工作正常
- (void)viewDidLoad {
[super viewDidLoad];
//Location
// create new location manager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
// start location manager
[self.locationManager startUpdatingLocation];
}
-(void) distanceBetweenUserandPin {
CLLocation *currentUserLocation = [[CLLocation alloc] initWithLatitude:_currentLocation.latitude longitude:_currentLocation.longitude];
CLLocation *currentPinLocation = [[CLLocation alloc] initWithLatitude:_pinLocation.latitude longitude:_pinLocation.longitude];
CLLocationDistance distanceBetweenUserAndPinMeters = [currentUserLocation distanceFromLocation:currentPinLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//This successfully saves Lat, Long Data to a point location
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude);
NSLog(@"%f, %f", location.latitude, location.longitude);
//This assigns the value of location to the ivar _currentLocation
_currentLocation = CLLocationCoordinate2DMake(location.latitude, location.longitude);
NSLog(@"%f, %f", _currentLocation.latitude, _currentLocation.longitude);
}
关于iphone - 我无法在一段时间内获取 gps 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6929081/