13

This is the code that I have in my AppDelegate Class

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = 1000;  // 1 Km
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    [locationManager startUpdatingLocation];
}

And this is the delegate method i have in my AppDelegate Class

    //This is the delegate method for CoreLocation
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{

        //printf("AppDelegate latitude %+.6f, longitude %+.6f\n", newLocation.coordinate.latitude, newLocation.coordinate.longitude);

}

Its working in 3.0, 3.1, 3.1.3 , but its not working in 4.0 simulator and device both.

What is the reason ?

4

5 回答 5

21

我有一个类似的错误,现在解决了。问题是在本地声明 locationManager 变量。将其声明为类变量,并确保直接或通过属性保留(如果使用 ARC,则为强)保留它。这样就解决了问题!问题是 de locationManager 变量被释放并且从未更新委托。这里的代码:

.h 文件:

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


@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> {
@private
    MKMapView *_mapView;
    CLLocationManager *_locationManager;
}

@property(nonatomic, strong) CLLocationManager *locationManager;

@end

.m 文件:

self.locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];

我希望它有所帮助。

于 2012-05-23T02:18:46.237 回答
2

-locationManager:didFailWithError:的代表有没有被叫过?我只是在想,也许您在某个时候拒绝访问位置数据,现在没有收到提示,但访问被拒绝。

于 2010-06-17T05:40:08.167 回答
2

我疯狂地试图弄清楚为什么只有几个使用 CLLocationManager 的类之一永远无法调用任何CLLocationManagerDelegate 方法。

事实证明:该类是从非主线程初始化的,因此它的 CLLocationManager 是在非主线程上创建的……它甚至可能是一个临时线程。无论如何,用下面的代码包装初始化我的类的调用就是让我的 CLLocationManager 委托方法被调用所需的全部内容。

dispatch_sync(dispatch_get_main_queue(), 
                    ^{
                         // create your class that will create a CLLocationManager here.
                     });

所以底线:不要在除主线程之外的任何东西上创建 CLLocationManager,否则你永远不会调用你的委托方法!我曾经知道这...只是很久...叹息。浪费了一天的时间来解决这个问题!由于我读过的所有 CLLocationManager SO 都没有提到过这个,所以我把它贴在这里。希望它可以帮助某人!

于 2019-03-11T19:21:10.903 回答
0

将代码放在“被推送到 navigationController”的 viewController 中。

我的代码是。

在 SomeViewController.h

#import <MapKit/MapKit.h>
@interface SomeViewController : UIViewController<CLLocationManagerDelegate>{
}
@property (nonatomic, strong) CLLocationManager *locationManager;
@end

在 SomeViewController.m

#import "SomeViewController.h"

@implementation SomeViewController


-(void)findLocation{
    if ([CLLocationManager locationServicesEnabled]) {
        if (!self.locationManager) {
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self;
            self.locationManager.distanceFilter = kCLDistanceFilterNone;
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            [self.locationManager startUpdatingLocation];
        }
    }
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    NSLog(@"didUpdateLocations");

    CLLocation *lo = [locations objectAtIndex:0];
    NSLog(@"latitude = %f, longitude = %f", lo.coordinate.latitude, lo.coordinate.longitude);

    [manager stopUpdatingHeading];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    NSLog(@"didUpdateToLocation");}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"didFailWithError");
    [manager stopUpdatingLocation];
}

我认为问题是因为 ARC,locationManager 被释放。

于 2013-09-11T01:46:48.583 回答
0

您确定您没有在某处解除位置管理器的分配吗?您的代码显示它已在 didFinishLaunching... 方法中分配,但随后被遗忘(尽管它仍然保留,因此应该仍然有效)。

您是否甚至看到初始弹出窗口询问您是否可以获取用户位置?

于 2010-06-17T05:05:43.657 回答