获取当前位置时如何避免空值?

时间:2013-05-07 21:06:48

标签: xcode null location core-location nslog

我想让用户获得当前位置,我在stackoverflow中找到了解决方案:How to find your current location with CoreLocation。 但是,尽管有这个解决方案,我在Nslog上收到了这条消息:  2013-05-07 23:57:56.962 XXXXX[1951:c07] Latitude:(null),Longitude:(null)

这就是我所做的:

在头文件中:

#import <CoreLocation/CoreLocation.h>
NSString *UsersCurrentLatitude;
NSString *UsersCurrentLongitude;
CLLocationManager *locationManager;
实施中的

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.

        UsersCurrentLongitude:[NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];
        UsersCurrentLatitude:[NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];

            locationManager = [[CLLocationManager alloc] init];
            locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
            locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
            [locationManager startUpdatingLocation];

        NSLog(@"Latitude:%@,Longitude:%@",UsersCurrentLatitude,UsersCurrentLongitude);

}

1 个答案:

答案 0 :(得分:1)

您在这里缺少一步,您需要启动位置服务,然后从代表处获取位置。您可能需要查看exemple from Apple或此tutorial