iphone - 我无法在一段时间内获取 gps 坐标

标签 iphone objective-c ios

我已经实现了使用 - (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/

相关文章:

ios - UIView 不会移动到新位置

ios - 有什么方法可以将 gsm radio 频率转换为 fm radio 频率?

ios - AppDelegate 不访问更新的结构变量 - Swift

iphone - iOS (iPhone) 开发 - 本地通知在应用程序未运行时随机触发

jquery - iPhone 使用 webkit-transform 翻译 3d

iphone - 从横向切换到纵向后,将 UIScrollView 保持在正确的位置

iphone - 通过 segue 发送自定义单元格

objective-c - 将触摸传递到 UIWebView 的一部分

objective-c - 子类不继承其功能

ios - swift 3 : Load GIF in UIWebView