objective-c获取当前位置并将其存储以供以后访问。时间问题?

时间:2013-07-01 14:15:56

标签: iphone objective-c location cllocationmanager

我想获取iphone上的当前位置并使用坐标进行某些函数调用。

现在我遇到的问题是,当我想访问坐标时,它们是空的。但是在委托函数上,它们以某种方式被检索,但没有存储......

doLocation中的NSLog没有显示任何值,但在locationmanager上显示在NSLog中。或者这可能是NSLog问题吗?

#pragma mark - Location handling

-(CLLocationCoordinate2D)doLocation {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    self.locationManager.distanceFilter = 50;
    [self.locationManager startUpdatingLocation];

    CLLocation *location = [self.locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    NSLog(@"debug: coordinates: %@", coordinate); 

    return coordinate; 
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    if (!oldLocation ||
        (oldLocation.coordinate.latitude != newLocation.coordinate.latitude &&
         oldLocation.coordinate.longitude != newLocation.coordinate.longitude)) {

            currentLat = newLocation.coordinate.latitude;
            currentLng = newLocation.coordinate.longitude;


        } else { // oldLocation
            currentLat = oldLocation.coordinate.latitude;
            currentLng = oldLocation.coordinate.longitude;
        }

    self.currentLoc = [[CLLocation alloc] initWithLatitude:currentLat longitude:currentLng];

    NSLog(@"debug: self.currentLoc: %@", self.currentLoc);

}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    NSLog(@"%@", error);
}

Debug out是:

  

2013-07-01 15:59:29.382 foobar [3793:c07] debug:coordinates:(null)   -doLocation 1:当前位置:

     

2013-07-01 15:59:35.399 foobar [3793:c07] debug:self.currentLoc:< + 51.50998000,-0.13370000> +/- 0.00m(速度-1.00 mps /航向-1.00)@ 7/1 / 13,3:59:35 PM中欧夏季时间

2 个答案:

答案 0 :(得分:1)

[self.locationManager startUpdatingLocation]方法中调用(CLLocationCoordinate2D) doLocation时; GPS开始搜索位置。此时它没有坐标信息;因此,当您尝试在同一方法的下一行代码中获取Coordinates值时,它会显示坐标的NULL值。

当位置更新时,会自动调用didUpdateToLocation,并使用您didUpdateToLocation方法中记录的设备的当前位置信息传递它。希望这是有道理的。

如果要存储坐标信息,只需在全局或共享变量类中声明一个结构,只需使用在didUpdateToLocation委托中收到的值进行分配即可。然后,您可以迟早在代码中的任何位置使用这些值。

答案 1 :(得分:1)

当委托激发时,您将获得该位置,因此在这一点上,您可以获取坐标,并存储在某个变量中并创建一个委托,告诉您选择新坐标的对象。使用设计模式创建此类(位置)。

样品: .H

#import <CoreLocation/CoreLocation.h>

@protocol LocationDelegate <NSObject>

@optional
-(void)didUpdateLocation;
-(void)statusChanged;
@end
@interface Location : CLLocationManager <CLLocationManagerDelegate>
{
    BOOL wantUpdate;
}
@property(strong, nonatomic) CLLocationManager *lm;
@property(strong, nonatomic) NSString *latitude;
@property(strong, nonatomic) NSString *longitude;
@property(assign,nonatomic) id delegate;
@property(assign,nonatomic) id delegateStatus;

+(id)shared;
-(void)start;
-(void)update;
@end

的.m

+ (id)shared {
    static Location *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

-(id)init
{
    self = [super init];

    if(self){
        lm = [[CLLocationManager alloc]init];
        lm.delegate = self;
        lm.purpose = @"Your purpose";
        lm.desiredAccuracy = kCLLocationAccuracyBest;
        wantUpdate = NO;
        delegate = self;
        delegateStatus = self;
    }
        return self;
    }

    -(void)start
    {
        wantUpdate = YES;
        [lm startUpdatingLocation];
    }

    -(void)update
    {
        [lm startUpdatingLocation];
    }

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        if(wantUpdate)
        {
            latitude = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
            longitude = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];

            [lm stopUpdatingLocation];
            [delegate didUpdateLocation];
        }

        [lm stopUpdatingLocation];
        wantUpdate = NO;
    }

    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        NSLog(@"Error while location  %@",error);
    }

    -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
        if(status == kCLAuthorizationStatusAuthorized)
        {
            if([delegateStatus respondsToSelector:@selector(statusChanged)])
                [delegateStatus statusChanged];
        }
    }