2012-11-06 103 views
0

我想獲取用戶當前位置。這是我的代碼無法獲取用戶在iOS 5中的當前位置+

// 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 

但它沒有返回任何位置更新。 也不要求用戶使用位置的權限。我的應用程序未在定位服務中列出。我如何在Location Serverices中添加我的應用程序? 以上代碼中的worong是什麼?

回答

0

我通過在.h &中聲明UserLocation * userLocation來解決此問題。 因爲我正在使用ARC,所以它取消分配userLocation,所以我沒有獲取位置更新。

相關問題