CLLocationManager无法获取用户在设备中的位置,但无法在模拟器中获取

时间:2014-06-16 07:13:23

标签: ios iphone objective-c cllocationmanager

我在我的iOS应用程序中使用CLLocationManager作为GPS跟踪系统。该应用程序可以正常使用模拟器的调试功能,但一旦构建在设备中,它就无法这样做。给我一个错误,"无法获得位置。"我就这样做了:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = NO;

然后启动日期在一个uibutton。任何帮助将受到严重赞赏。谢谢。

1 个答案:

答案 0 :(得分:1)

首先设置位置管理器

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

之后调用委托方法

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);

    currentLocationCoordinate.latitude = location.coordinate.latitude;
    currentLocationCoordinate.longitude = location.coordinate.longitude;

    [[NSNotificationCenter defaultCenter] postNotificationName:@"Address Found" object:self];

}

这对我有用。它会帮助你