gpt4 book ai didi

ios - CoreLocation 管理器不更新位置

转载 作者:行者123 更新时间:2023-12-01 18:24:17 25 4
gpt4 key购买 nike

我试图实现一个单独的类来管理我的位置。每当我单击按钮时,我都想获得我的位置。
gpsFilter.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface gpsFilter : NSObject <CLLocationManagerDelegate>

@property (nonatomic, retain) CLLocationManager *gpsManager;
@property (nonatomic, retain) NSString * latitude;
@property (nonatomic, retain) NSString * longitude;
@end
gpsFilter.m .
#import "gpsFilter.h"

@implementation gpsFilter

- (id) init{
self = [super init];
if(self != nil){
self.gpsManager = [[CLLocationManager alloc] init];
self.gpsManager.delegate = self;
[self.gpsManager startUpdatingLocation];
BOOL enable = [CLLocationManager locationServicesEnabled];
NSLog(@"%@", enable? @"Enabled" : @"Not Enabled");
}
return self;
}

- (void)gpsManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if(currentLocation != nil){
self.latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
self.longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}

- (void)gpsManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
@end

我正在使用一个单独的类,因为我想在将来添加平滑过滤器。我没有得到任何更新。 NSLog 永远不会被触发。我认为某些变量正在自动释放,但我不确定是哪一个。

viewController 代码如下。
#import "gpstestViewController.h"

@interface gpstestViewController (){

}

@end

@implementation gpstestViewController


- (void)viewDidLoad
{
[super viewDidLoad];
self.location = [[gpsFilter alloc] init];
// Do any additional setup after loading the view, typically from a nib.
}


- (IBAction)getloca:(id)sender {
self.latitudeLabel.text = [self.location latitude];
self.longitudeLabel.text = [self.location longitude];

}

- (IBAction)getLocation:(id)sender {
self.latitudeLabel.text = [self.location latitude];
self.longitudeLabel.text = [self.location longitude];

}
@end

很抱歉,我倾倒了很多代码,但我是 ios 编程的新手,我无法找到问题所在。

编辑:根本没有调用 updateLocation 委托(delegate)方法。

最佳答案

委托(delegate)方法必须命名为 locationManager:didUpdateToLocation:fromLocation:locationManager:didFailWithError: .

您不能使用自定义方法名称,如 gpsManager:didUpdateToLocation:fromLocation: .

另请注意 locationManager:didUpdateToLocation:fromLocation:自 iOS 6 起已弃用,您应使用 locationManager:didUpdateLocations: .

关于ios - CoreLocation 管理器不更新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14858298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com