0

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

    // 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 中添加我的应用程序?上面的代码有什么问题?

4

1 回答 1

0

我通过在 .h 和写入属性中声明 UserLocation *userLocation 解决了这个问题。由于我使用的是 ARC ,它释放了 userLocation ,所以我没有得到位置更新。

于 2012-11-06T15:20:28.143 回答