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 に答える