我正在尝试在我的iOS项目中使用CLLocationManager
框架来访问用户的位置,但是当我打电话时
[locationManager startUpdatingLocation]
locationManager:didUpdateLocations:
或locationManager:didFailWithError:
都没有被召集。
//myViewController.h
@interface myViewController : UITableViewController <CLLocationManagerDelegate>
@end
//myViewController.m
@implementation myViewController{
CLLocationManager *locationManager;
}
//edit
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
}
//finish edit
-(void)getLocation
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Failed to Get Your Location"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = locations[[locations count] -1];
CLLocation *currentLocation = newLocation;
NSString *longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
if (currentLocation != nil) {
NSLog(@"latitude: %@", latitude);
NSLog(@"longitude: @"%@", longitude);
}else {
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
}
}
@end
尽管在文档中说明了,但是没有调用委托方法:
“此方法立即返回。调用此方法会导致位置管理器获取初始位置修复(可能需要几秒钟)并通过调用其locationManager:didUpdateLocations:
方法通知您的委托[...]除了实现locationManager:didUpdateLocations:
方法的委托对象之外,它还应该实施locationManager:didFailWithError:
方法来应对潜在的错误。“
不知道如何调试问题。
谢谢,
与
只需在info.plist中添加它即可
NSLocationAlwaysUsageDescription ---我需要位置
NSLocationWhenInUseUsageDescription ---我需要位置
隐私 - 位置使用说明---我需要位置
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager requestWhenInUseAuthorization];
[locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
现在它肯定会调用你的didUpdateToLocation。
了解更多详情click here
从iOS 8开始,位置服务的工作方式略有不同。
主要是,您需要将一个密钥NSLocationWhenInUseUsageDescription
添加到您的Info.plist文件中,并添加一个描述您的应用程序需要位置的原因,例如“需要位置...”。
请注意,您可能还需要检查iOS版本。只有iOS 8及更高版本的位置管理器才能收听requestWhenInUseAuthorization
电话。
以下链接显示更多详细信息:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
祝好运!
您需要检查是否在主线程上初始化CLLocationManager。在其他情况下,您将无法获得updateLocation事件。
我在Apple文档中找不到这样的信息,但无论如何它对我有用。
使用iOS 8.0,您需要先调用-[CLLocationManager requestWhenInUseAuthorization
]
或-[CLLocationManager requestAlwaysAuthorization]
,以便要求用户授予您的应用程序使用该位置的权限。
您需要在项目中添加以下内容,
Key: NSLocationAlwaysUsageDescription Type:String
Key: NSLocationWhenInUseUsageDescription Type:String
[locationManager startUpdatingLocation]
的.m文件中添加以下条件:
if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
[locationManager requestAlwaysAuthorization];`
然后只会调用你的CLLocationManager
委托方法。
我想你应该检查这个链接。你在声明时没有保留位置管理器对象。
请给你物业@property(非原子,强)
Why the CLLocationManager delegate is not getting called in iPhone SDK 4.0?
我的代码库中有以下内容:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if( status == kCLAuthorizationStatusAuthorizedAlways ) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 200;
}
}
事实证明这是在一个无限循环中被调用,因为初始化一个locationManager由于某种原因触发了这个方法?我没有意识到这一点。当授予权限时,我已经把它放在那里。相反,我设置了一个位置管理器并开始更新位置,但随后将其触发并将其替换为未更新位置的位置管理器,并一遍又一遍地循环。
我的解决方案就是将&& !_locationManager
添加到if条件中。
因此,如果您在模拟器中运行,请不要忘记在模拟器菜单中设置位置。看起来如果设置为none,则委托中不会调用任何内容...我不确定是否这可能会发生在真实设备上,但可能是模拟器特定的。