我目前正在开发我的第一个基于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个小时。
答案 0 :(得分: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];
}
}
答案 1 :(得分:1)
对于我,在Info.plist中将字符串“location-services”添加到UIRequiredDeviceCapabilities数组解决了这个问题。
UIRequiredDeviceCapabilities的值是一个字符串数组,表示您的应用所需的功能。有两个与位置服务相关的字符串:
如果您需要一般的位置服务,请包含location-services字符串。 如果您的应用需要仅由GPS硬件提供的准确度,请包含gps字符串。
答案 2 :(得分:0)
Duh,愚蠢的我!
我忘了在.h文件而不是.m文件中声明LocationGetter实例的变量!