无法在iOS 5 +中获取用户当前位置

时间:2012-11-06 08:50:03

标签: ios5 gps core-location cllocationmanager

我想让用户获得当前位置。这是我的代码

    // In LoginViewController.h

@interface LoginViewController : UIViewController <UserLocationDelegate,UIAlertViewDelegate> {

    CLLocation *usersLocation;
}
@property (nonatomic,strong) CLLocation *usersCurrentLocation;
@end


// In LoginViewController.m

@implementation LoginViewController

@synthesize usersCurrentLocation;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.usersCurrentLocation = nil;
    [self currentLocationOfUser];
}

-(void)currentLocationOfUser {

    UserLocation *userLocation = [[UserLocation alloc]init];//UserLocation];
    userLocation.delegate = self;
    [userLocation getCurrentLocationOfUser];
}

#pragma mark - User Location Delegate Methods

- (void)locationUpdate:(CLLocation *)location
{
    self.usersCurrentLocation = location;
    NSLog(@"Latitude:- %.6f",self.usersCurrentLocation.coordinate.latitude);
    NSLog(@"Longitude:- %.6f",self.usersCurrentLocation.coordinate.longitude);

}

- (void)locationError:(NSString *)errorMsg
{
    [Common showAlertWithTitle:@"Error" andMessage:errorMsg];
}


// In UserLocation.h


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

@protocol UserLocationDelegate <NSObject>

@required
- (void)locationUpdate:(CLLocation *)location; 
- (void)locationError:(NSString *)errorMsg;

@end

@interface UserLocation : NSObject <CLLocationManagerDelegate> {

    CLLocationManager *locationManager;
    __weak id<UserLocationDelegate>delegate;

}

@property (nonatomic,strong) CLLocationManager *locationManager;
@property (nonatomic,weak) id<UserLocationDelegate>delegate;

-(id)initUserLocation;
-(void)getCurrentLocationOfUser;

@end

// In UserLocation.m

#import "UserLocation.h"

@implementation UserLocation

@synthesize locationManager;
@synthesize delegate;
@synthesize geoCodingDelegate;

-(id)initUserLocation 
{
    if (self == [super init]) {
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    }
    return self;
}

-(void)getCurrentLocationOfUser {

    self.locationManager = [[CLLocationManager alloc]init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

    if ([CLLocationManager locationServicesEnabled]) {
        //[self.locationManager startMonitoringSignificantLocationChanges];
        [self performSelector:@selector(startLocationUpdate) withObject:nil afterDelay:2.0];
    }
    else {
        if ([delegate respondsToSelector:@selector(locationError:)]) {
            [self.delegate locationError:@"Please Turn On Location Services in Settings To Retrive User's Current Location"];
        }
        else {
            [Common showAlertWithTitle:@"Error" andMessage:@"Please Turn On Location Services in Settings To Retrive User's Current Location"];
        }
    }
}

-(void)startLocationUpdate
{
    [self.locationManager startMonitoringSignificantLocationChanges];
}

#pragma mark CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    if ([delegate respondsToSelector:@selector(locationUpdate:)]) {
        [delegate locationUpdate:newLocation];
    }
}

-(void)locationManager:(CLLocationManager *)manager 
      didFailWithError:(NSError *)error
{
    if ([delegate respondsToSelector:@selector(locationError:)]) {
        [delegate locationError:@"Some Error Occured While Retriving Users's Location"];
    }
    else {
        [Common showAlertWithTitle:@"Error" andMessage:@"Some Error Occured While Retriving Users's Location"];
    }
}

@end

但它没有返回任何位置更新。 它也没有要求用户使用位置的权限。我的应用未列在位置服务中。如何在位置服务器中添加我的应用程序? 上面的代码有什么用?

1 个答案:

答案 0 :(得分:0)

我通过在.h&amp;中声明UserLocation * userLocation解决了这个问题。写财产。 由于我使用ARC,因此我取消分配userLocation,因此我没有获得位置更新。