2010-06-17 60 views
12

这是我的代码在我AppDelegate类为什么在iPhone SDK 4.0中未调用CLLocationManager委托?

- (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]; 
} 

这是委托方法我在我的AppDelegate类

//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); 

} 

其在3.0工作,3.1,3.1.3,但它不适用于4.0模拟器和设备。

是什么原因?

+0

感谢@Scott Christopherson提供了一些积极的方法.....但是我们仍然成为木偶,每次有新的iPhone SDK发布时都必须更改我们的代码。总是会发现某些方法已被弃用或无法正常工作... – Biranchi 2010-06-17 05:07:13

+0

Thanks @Scott Christopherson请投票结束这个问题....因为没有人可以解决这个问题... – Biranchi 2010-06-17 05:08:34

+0

面对同样的问题,你确定需要40秒才能更新位置吗? – 2010-07-08 06:03:14

回答

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]; 

我希望它能帮助。

0

你确定你没有在某处释放位置管理器吗?你所拥有的代码显示它被分配在didFinishLaunching ...方法中,但却被遗忘了(虽然它仍然保留,所以仍然可以工作)。

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

+0

是的尝试了所有可能的解决方案.... 我需要重新安装iPhone SDK吗? ? – Biranchi 2010-06-17 05:09:47

2

您的委托中有没有-locationManager:didFailWithError:被叫?我只是在想,也许你在某个时候拒绝了对位置数据的访问,现在没有得到提示,但访问被拒绝。

0

将代码放入一个viewController“,它被推送到navigationController”。

我的代码是。

在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被释放。

相关问题