gpt4 book ai didi

ios - iOS位置问题-看不到任何数据

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

我是iOS编程的新手,正在尝试获取当前位置。我的主要应用程序代表是这样的,

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

@class AWSViewController;

@interface AWSAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) AWSViewController *viewController;
@property (strong, nonatomic) UINavigationController *navController;
@property (strong, nonatomic) NSMutableArray *cities;

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

@end

在实现中,我有这两种方法,我从 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions调用getCurrentLocation

我可以在控制台上看到“开始获取位置”字样,此后什么也看不到。
-(void)getCurrentLocation {

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.distanceFilter = 500;
[locationManager startUpdatingLocation];
NSLog(@"Started to get locations");
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

CLLocation *location = [locations lastObject];
NSLog(@"Got Location");
NSLog(@"latitude %+.6f, longitude %+.6f\n", location.coordinate.latitude, location.coordinate.longitude);
}

我在这里做错了什么?
谢谢

最佳答案

代码退出getCurrentLocation方法后,您的位置管理器就会超出范围。将位置管理器设置为ivar,以使其保持存在状态。

@interface AWSAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}



-(void)getCurrentLocation {
locationManager = [[CLLocationManager alloc] init];
// etc
}

关于ios - iOS位置问题-看不到任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18511191/

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