1

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

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

NSLog 中doLocation不显示任何值,但在locationmanagerNSLog 中显示。或者这可能是一个 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);
}

调试出来的是:

2013-07-01 15:59:29.382 foobar[3793:c07] 调试:坐标:(空)-doLocation 1:当前位置:

2013-07-01 15:59:35.399 foobar[3793:c07] 调试:self.currentLoc:<+51.50998000,-0.13370000> +/- 0.00m(速度 -1.00 mps / 课程 -1.00)@ 7/1/13 , 下午 3:59:35 中欧夏令时间

4

2 回答 2

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];
        }
    }
于 2013-07-01T19:22:34.237 回答
1

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

并且当位置更新时,didUpdateToLocation会自动调用它,并与正在您的didUpdateToLocation方法中记录的设备的当前位置信息一起传递。希望这会有意义。

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

于 2013-07-01T14:34:20.287 回答