Ios 与位置经理的目标C问题

Ios 与位置经理的目标C问题,ios,objective-c,Ios,Objective C,我正在使用位置管理器。它的行为很奇怪。我把这些UILabel放在适当的位置,我希望在坐标发生变化时不断更新这些UILabel。这就是我所拥有的: @implementation ViewController { CLLocationManager *locationManager; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; locationManager = [[C

我正在使用位置管理器。它的行为很奇怪。我把这些UILabel放在适当的位置,我希望在坐标发生变化时不断更新这些UILabel。这就是我所拥有的:

@implementation ViewController
{
    CLLocationManager *locationManager;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];


    locationManager = [[CLLocationManager alloc] init];

    [locationManager requestWhenInUseAuthorization];

    [locationManager requestAlwaysAuthorization];

    currentLocation = nil;

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
}
然后这个:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *) newLocation
           fromLocation:(CLLocation *)oldLocation
{
    /*if (currentLocation == nil)
        currentLocation = newLocation;

    if (currentLocation != nil) {
        Longitude = [NSString stringWithFormat:@"%.10f", currentLocation.coordinate.longitude];
        Latitude = [NSString stringWithFormat:@"%.10f", currentLocation.coordinate.latitude];

        LongitudeDouble = currentLocation.coordinate.longitude;
        LatitudeDouble = currentLocation.coordinate.latitude;
    }*/

    currentLocation = newLocation;

    Longitude = [NSString stringWithFormat:@"%.10f", currentLocation.coordinate.longitude];
    Latitude = [NSString stringWithFormat:@"%.10f", currentLocation.coordinate.latitude];

    LongitudeDouble = currentLocation.coordinate.longitude;
    LatitudeDouble = currentLocation.coordinate.latitude;

    Long.text = [NSString stringWithFormat:@"%.10f", LongitudeDouble];
    Lat.text = [NSString stringWithFormat:@"%.10f", LatitudeDouble];

}
我的问题是,当我运行应用程序时,标签有时会更新,但一段时间后,标签会停止,当我关闭应用程序并重新打开时,标签根本不会更新


我做错了什么?

可能是您应该添加此行
self.locationManager.distanceFilter=0.1

然后像这样修复代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];

    [locationManager requestWhenInUseAuthorization];

    [locationManager requestAlwaysAuthorization];

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = 0.1
    [locationManager startUpdatingLocation];
}

    - (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    currentLocation = nil;  
}
试试这个

viewDidLoad:

locationManager =[[CLLocationManager alloc] init] ;
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
Info.plist:

NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
然后


仅当从旧位置更新到新位置时才会调用它。您可以检查在
viewDidAppear
locationManager:didUpdateLocation:fromLocation:
之间首先调用的方法是ViewDidAppartry此方法
func locationManager(manager:CLLocationManager,didUpdateLocations locations:[CLLocation])
不是很快吗?我在objective cIt中执行此操作,在objective C中也有方法
locationManager:didUpdateLocations:
,我应该何时调用此方法?不,此方法不起作用。我的位置没有持续更新,几乎就像被锁定了一样,不会改变。
 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

NSLog(@"failed to update location");
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

 CLLocation *currentLocation=[locations lastObject];

 lat=currentLocation.coordinate.latitude;
 lon=currentLocation.coordinate.longitude;

 NSString *strLat=[NSString stringWithFormat:@"%f",lat];
 NSString *strLon=[NSString stringWithFormat:@"%f",lon];

 latitude_1=[[NSMutableString alloc] initWithString:strLat];
 longitude_1=[[NSMutableString alloc] initWithString:strLon];  
}