我正在iphone sdk4.0上创建一个应用程序。在那里,确实没有调用更新位置方法。 我在下面给出了我的代码。请帮助。谢谢。
-(id)init
{
[super init];
obj=[[UIApplication sharedApplication]delegate];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
return self;
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%f",newLocation.coordinate.latitude);
NSLog(@"%f",newLocation.coordinate.longitude);
obj=[[UIApplication sharedApplication]delegate];
location=[[CLLocation alloc]initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
obj.lattitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
obj.longitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
//location=[[CLLocation alloc]initWithLatitude:39.9883 longitude:-75.262227];
}
答案 0 :(得分:18)
此外,在iOS8中,你必须有两件额外的东西:
在Info.plist中添加一个密钥,并请求位置管理员授权启动。
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
您需要请求相应位置方法的授权。
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
代码示例:
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
来源:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
答案 1 :(得分:5)
嗨,您的代码似乎没问题。 现在这些可能是原因:
你的init上的:尝试检查是否有locationServicesEnabled。
locationManager = [[CLLocationManager alloc] init];
if(locationManager.locationServicesEnabled == NO){
//Your location service is not enabled, Settings>Location Services
}
其他原因,您可能无法获取应用的位置信息。 解决方案:只需从iPhone中删除您的应用程序并重新构建,现在它应该弹出对话框以允许位置。
用它来检查错误
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Error while getting core location : %@",[error localizedFailureReason]);
if ([error code] == kCLErrorDenied) {
//you had denied
}
[manager stopUpdatingLocation];
}
否则一切似乎都好, 你已经在运行ios 4.0,它可以安装在iPhone 3G及更高版本中, 如果是iPhone 2g,则可能会出现此问题。
答案 2 :(得分:4)
确保将CLLocationManager添加为属性。
@property(非原子,强)CLLocationManager * locationManager;
答案 3 :(得分:3)
如果您在模拟器上测试它,它将无法正常工作。
这是因为该方法仅在设备更改位置时调用(并且模拟器永远不会更改位置)。
答案 4 :(得分:1)
您的代理中的-locationManager:didFailWithError:
是否会被调用?也许您在某些时候拒绝访问位置数据,现在不会收到提示,但访问被拒绝。
答案 5 :(得分:0)
在您的位置管理员alloc
和init
之后,请检查是否允许位置服务。此代码来自Apple的“LocateMe”代码示例
if (locationManager.locationServicesEnabled == NO) {
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[servicesDisabledAlert show];
[servicesDisabledAlert release];
}
如果您正在使用模拟器进行测试,请尝试使用设备进行测试。模拟器要求打开计算机的WiFi,并且它可以定位位于其已知位置的数据库中的WiFi基站。