2012-06-20 32 views
0

我目前正在開發我的第一個基於CoreLocation的應用程序,並且我沒有調用委託方法的CLLocationManager的問題。這裏是我正在使用的課程:CoreLocation:didUpdateToLocation沒有被調用

// 
// 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個小時。

+0

這就像在你的方法中設置delegate = self一樣簡單嗎?如果正確實施,我無法從頭文件中知道。 –

+0

@EvanDyson它在那裏:'self.locationManager.delegate = self;' –

+0

啊,沒有滾動。道歉。顯示沒問題,但查看更多。 –

回答

2

使用我的代碼,它應該爲你工作...

首先確保您使用的是協議中加入以下,例如您的AppDelegate或任何控制器,你要CoreLocation在工作...

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate> 

創建一些屬性,我還使用自定義的方法,但你不必太...

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

- (void)startUpdatingCurrentLocation; 

然後在您的實現文件調用自定義實現方法具d開始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]; 
    } 
} 
0

杜,笨!

我忘了在.h文件中聲明LocationGetter實例的變量而不是.m文件!

1

對於我添加字符串「location-services」到Info.plist中的UIRequiredDeviceCapabilities數組解決了這個問題。

閱讀文檔這裏 https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1

爲UIRequiredDeviceCapabilities值是表示你的應用程序需要的特徵字符串數組。有兩個字符串與位置服務相關:

如果您需要位置服務,請包含位置服務字符串。 如果您的應用需要僅由GPS硬件提供的精度,請包含GPS字符串。