0

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

//
//  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 个小时试图找出问题所在。

4

3 回答 3

2

使用我的代码,它应该对你有用......

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

@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];
  }
}
于 2012-06-20T20:38:08.697 回答
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 字符串。

于 2013-10-31T01:27:58.550 回答
0

呸呸呸呸呸!

我忘记在 .h 文件而不是 .m 文件中声明 LocationGetter 实例的变量!

于 2012-06-20T21:27:43.280 回答