位置管理器未获取位置
Location Manager not getting location
我正在尝试将我的一个应用程序更新到 IOS9 和 Xcode 7.2.1。位置管理器在早期版本中工作,但现在不工作。没有错误或警告......没有。看来我的代码没有达到 "didUpdateLocations"。我的代码在下面;
- (void)setupViewController {
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization];
}
locationManager.desiredAccuracy = 100;
//locationManager.requestLocation;
[locationManager startUpdatingLocation];
//return coordinate;
[self performSelector:@selector(stopUpdatingLocation:) withObject:@"Timed Out" afterDelay:5];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"Doesn't get here.");
self.bestEffortAtLocation = newLocation;
latitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
longitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];
There is other code here that gets city, country, etc.
此时不知道该怎么做。我已经寻找过类似的问题,但没有找到任何东西。
像这样尝试。
- (void)setupViewController
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 100;
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[self.locationManager startUpdatingLocation];
[self.locationManager requestWhenInUseAuthorization];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.bestEffortAtLocation = newLocation;
latitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
longitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];
[self.locationManager stopUpdatingLocation];
}
从 setupViewController
中删除您的 performselector
方法,您必须使用所有地方 self
。希望对你有帮助。
问题出在这一行:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
永远不会调用该方法,因为它对应于 delegate method。你需要实现这个方法:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations {
更新您的 info.plist 文件。
<key>NSLocationWhenInUseUsageDescription</key>
<string>This application requires location services to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>
我正在尝试将我的一个应用程序更新到 IOS9 和 Xcode 7.2.1。位置管理器在早期版本中工作,但现在不工作。没有错误或警告......没有。看来我的代码没有达到 "didUpdateLocations"。我的代码在下面;
- (void)setupViewController {
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization];
}
locationManager.desiredAccuracy = 100;
//locationManager.requestLocation;
[locationManager startUpdatingLocation];
//return coordinate;
[self performSelector:@selector(stopUpdatingLocation:) withObject:@"Timed Out" afterDelay:5];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"Doesn't get here.");
self.bestEffortAtLocation = newLocation;
latitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
longitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];
There is other code here that gets city, country, etc.
此时不知道该怎么做。我已经寻找过类似的问题,但没有找到任何东西。
像这样尝试。
- (void)setupViewController
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 100;
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[self.locationManager startUpdatingLocation];
[self.locationManager requestWhenInUseAuthorization];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.bestEffortAtLocation = newLocation;
latitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
longitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];
[self.locationManager stopUpdatingLocation];
}
从 setupViewController
中删除您的 performselector
方法,您必须使用所有地方 self
。希望对你有帮助。
问题出在这一行:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
永远不会调用该方法,因为它对应于 delegate method。你需要实现这个方法:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations {
更新您的 info.plist 文件。
<key>NSLocationWhenInUseUsageDescription</key>
<string>This application requires location services to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>