1

My custom class needs to receive the "didUpdateToLocation" CLLocationManagerDelegate method, however i can't seem to make the following code work.

Header file.

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

@interface MyCurrentLocation : NSObject<MKAnnotation,CLLocationManagerDelegate,MKMapViewDelegate>
{
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) CLLocationManager *locationManager;

-(MyCurrentLocation *)init;

@end

Implementation file.

#import "MyCurrentLocation.h"

@implementation MyCurrentLocation

-(BLCurrentLocation *)init
{
    self = [super init];
    if (self) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [_locationManager startUpdatingLocation];
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"Did we receive a location?");
}
4

2 回答 2

1

我不知道为什么,但也许您可以尝试实现此委托方法以查看是否无法注册位置服务。

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please make sure Location Service is ON" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alertView show]; 
}
于 2012-09-22T05:59:54.477 回答
1

您必须响应其他委托方法,例如错误回调。此外,检查 CLLocationManager 的 + (CLAuthorizationStatus)authorizationStatus 类方法以确定您是否能够使用位置服务。

于 2012-09-22T06:02:22.127 回答