ja.*_*ja. 37 objective-c cllocationmanager ios
我正在尝试使用CLLocationManager
我的iOS项目中的框架来访问用户的位置,但是当我打电话时
[locationManager startUpdatingLocation]
Run Code Online (Sandbox Code Playgroud)
既不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
Run Code Online (Sandbox Code Playgroud)
尽管在文档中说明了,但是没有调用委托方法:
"此方法立即返回.调用此方法会导致位置管理器获取初始位置修复(可能需要几秒钟)并通过调用其locationManager:didUpdateLocations:
方法通知您的委托[...]除了实现该locationManager:didUpdateLocations:
方法的委托对象之外,它还应该实施这种locationManager:didFailWithError:
方法来应对潜在的错误."
不知道如何调试问题.
谢谢,
JA
Moh*_*mar 89
只需在info.plist中添加它即可
NSLocationAlwaysUsageDescription ---我需要位置
NSLocationWhenInUseUsageDescription ---我需要位置
隐私 - 位置使用说明---我需要位置
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager requestWhenInUseAuthorization];
[locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)
现在它肯定会调用你的didUpdateToLocation.
有关详细信息,请单击此处
小智 13
从iOS 8开始,位置服务的工作方式略有不同.
主要是,你需要一个键添加NSLocationWhenInUseUsageDescription
到您的Info.plist文件,并添加说明为什么你的应用程序需要的位置,如"需要位置...".
请注意,您可能还需要检查iOS版本.只有iOS 8及更高版本才能让位置管理器收听该requestWhenInUseAuthorization
呼叫.
以下链接显示更多详细信息:http: //nevan.net/2014/09/core-location-manager-changes-in-ios-8/
祝好运!
mal*_*lex 11
您需要检查是否在主线程上初始化CLLocationManager.在其他情况下,您将无法获得updateLocation事件.
我在Apple文档中找不到这样的信息,但无论如何它对我有用.
使用iOS 8.0,您需要先致电-[CLLocationManager requestWhenInUseAuthorization
]
或-[CLLocationManager requestAlwaysAuthorization]
首先要求用户授予您的应用程序使用该位置的权限.
归档时间: |
|
查看次数: |
39862 次 |
最近记录: |