gpt4 book ai didi

objective-c - 核心位置 : didUpdateToLocation not getting called

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

我目前正在开发我的第一个基于 CoreLocation 的应用程序,但我遇到了 CLLocationManager 不调用委托(delegate)方法的问题。这是我正在使用的类(class):

//
// LocationGetter.h
// whatsunderme
//
// Created by Tobias Timpe on 16.06.12.
// Copyright (c) 2012 tobisoft. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationGetterDelegate
@required
- (void) newPhysicalLocation:(CLLocation *)location;
@end

@interface LocationGetter : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
id delegate;
}

- (void)startUpdates;

@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) id delegate;
@end

//
// LocationGetter.m
// whatsunderme
//
// Created by Tobias Timpe on 16.06.12.
// Copyright (c) 2012 tobisoft. All rights reserved.
//

#import "LocationGetter.h"

@implementation LocationGetter

@synthesize locationManager, delegate;
BOOL didUpdate = NO;

- (void)startUpdates {
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
NSLog(@"Starting Location Updates");
locationManager.distanceFilter = 100;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Error");
}

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)
newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"new Location found");
didUpdate = YES;
// Disable future updates to save power.
[self.locationManager stopUpdatingLocation];

[self.delegate newPhysicalLocation:newLocation];

}


@end

didUpdateToLocation 和 didFailWithError 方法都没有被调用。我不知道为什么。希望你能帮我解决这个问题,因为我已经尝试找出问题所在 2 小时了。

最佳答案

使用我的代码,它应该适合你...

首先通过添加以下内容确保您正在使用该协议(protocol),例如添加到您的 AppDelegate 或您希望 CoreLocation 在其中工作的任何 Controller ...

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>

创建一些属性,我也使用自定义方法,但你没有...

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *currentLocation;

- (void)startUpdatingCurrentLocation;

然后在你的实现文件中调用自定义方法启动CL

// start location services
[self startUpdatingCurrentLocation];

下面是我的自定义方法的代码。

- (void)startUpdatingCurrentLocation
{
// if location services are on
if([CLLocationManager locationServicesEnabled])
{
// if location services are restricted do nothing
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted )
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Determining your current location cannot be performed at this time because location services are enabled but restricted." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
else
{
if(!locationManager_)
{
locationManager_ = [[CLLocationManager alloc] init];
[locationManager_ setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager_ setDelegate:self];
[locationManager_ setDistanceFilter:5.0f]; // measured in meters
[locationManager_ setHeadingFilter:5]; // measured in degrees
[locationManager_ setPurpose:@"Enabling location services is required in order to automatically determine the location of yada yada yada..."];
}

[locationManager_ startUpdatingLocation];
}
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Determining your current location cannot be performed at this time because location services are not enabled." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}

关于objective-c - 核心位置 : didUpdateToLocation not getting called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11126426/

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