gpt4 book ai didi

iphone - 停止更新位置后无法获得新位置 - IOS

转载 作者:行者123 更新时间:2023-11-28 22:34:34 26 4
gpt4 key购买 nike

我正在尝试从我的应用程序中获取用户的 GPS 坐标,该应用程序一次运行良好。当调用 View 时,我运行代码并获取位置 - 很好 - 但是当我返回 View 时,我想要新坐标,但我只得到旧坐标,就像它们在手机中被打碎一样。

这是我的代码:

#import "PostPictureViewController.h"

@interface PostPictureViewController ()

@end

@implementation PostPictureViewController

{
CLLocationManager *locationManager;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
}

-(void) viewDidAppear:(BOOL)animated{
locationManager = [[CLLocationManager alloc] init];
[self getLocation];
}

-(void)getLocation{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Fel" message:@"Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
NSLog([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
NSLog([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
}
// Stop Location Manager
[locationManager stopUpdatingLocation]; //This is screwing it up
locationManager = nil;
}

.h

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

@interface PostPictureViewController : UIViewController<CLLocationManagerDelegate>

@end

我认为我的问题是 [locationManager stopUpdatingLocation]; 行,我在第一次获取坐标后停止了 locationManager。我试过没有这条线然后它工作 - 更新坐标 - 但我不希望 locationmanager 每秒向我发送新坐标。我每次观看时只需要一次。

有人知道吗?提前致谢

最佳答案

这样试试:

- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[self getLocation];
}

-(void) viewWillAppear:(BOOL)animated{
[locationManager startUpdatingLocation];
}

-(void)getLocation{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Fel" message:@"Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
NSLog([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
NSLog([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
}
// Stop Location Manager
[locationManager stopUpdatingLocation]; //This is screwing it up
}

- (void)viewDidUnload
{
locationManager = nil;
}

关于iphone - 停止更新位置后无法获得新位置 - IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16443135/

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