Iphone 我能';t在一段持续的时间内获取gps坐标
我已经使用Iphone 我能';t在一段持续的时间内获取gps坐标,iphone,objective-c,ios,Iphone,Objective C,Ios,我已经使用-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation实现了从gps检索坐标的标准方法 问题是该函数仅在初始化时调用,而不是在程序的整个生命周期内调用。这正常吗 在安卓系统中,你可以实现一个监听器,你可以立即获得数据 我不是这样做的吗?如果是,会有什么问题?(顺便说一句,我已经
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation实现了从gps检索坐标的标准方法
问题是该函数仅在初始化时调用,而不是在程序的整个生命周期内调用。这正常吗
在安卓系统中,你可以实现一个监听器,你可以立即获得数据
我不是这样做的吗?如果是,会有什么问题?(顺便说一句,我已经检查过了,我没有停止更新位置)
我有一个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的距离。首先,我觉得奇怪的是,您会使用CLLocationManager的子类,因为我不确定这会给您带来什么好处。假设这不是问题,但是
从CLLocationManager
:
此方法立即返回。调用此方法会导致
location manager获取初始位置修复(可能需要
几秒钟)并通过调用其
locationManager:didUpdateToLocation:fromLocation:method。之后,
接收方主要在
超出了distanceFilter属性。更新可能以其他方式提供
尽管如此。例如,接收器可以发送另一个信号
如果硬件收集到更准确的位置读数,则发出通知
发生的情况是,它被调用一次用于初始位置定位,但它不会再次调用,因为其他条件没有改变。如果用户没有移动到任何地方,则不会提供新的位置数据,因为它将与上次相同(文档中提到的一些例外情况)
在测试应用程序时,请确保尝试四处移动并更改位置以生成更新。如果不起作用,请尝试使用desiredAccuracy
和distanceFilter
属性:
您可以通过调用
startUpdatingLocation方法。此服务最适合于
需要对交付进行更细粒度控制的应用程序
地点事件。具体来说,它考虑了
desiredAccuracy and distanceFilter属性以确定何时
发布新事件
除此之外,我想这可能与您如何子类化CLLocationManager有关。提供一些代码可能会有所帮助。同意@Matt的观点,没有更多的代码,我能提供的最佳解决方案是告诉它在每次移动设备时进行更新:
[self.locationManager setDistanceFiler:kCLDistanceFilterNone]
更新
我浏览了过去的项目,发现了我相信您正在寻找的代码,假设您的location manager子类工作正常
- (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);
}