Iphone 从未调用didUpdateLocation方法
我正在iphonesdk4.0上制作一个应用程序。在这个应用程序中,从未调用过更新位置方法。 我已经在下面给出了我的代码。请帮助。提前谢谢Iphone 从未调用didUpdateLocation方法,iphone,ios,ios4,cllocationmanager,cllocation,Iphone,Ios,Ios4,Cllocationmanager,Cllocation,我正在iphonesdk4.0上制作一个应用程序。在这个应用程序中,从未调用过更新位置方法。 我已经在下面给出了我的代码。请帮助。提前谢谢 -(id)init { [super init]; obj=[[UIApplication sharedApplication]delegate]; locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self;
-(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];
}
您的代理中的
-locationManager:didFailWithError:
是否曾被调用?可能您在某个时候拒绝访问位置数据,现在没有得到提示,但访问被拒绝。您好,您的代码似乎正常。
现在可能有以下原因:
在您的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,则可能会出现此问题。在您的位置管理器
alloc
和init
之后,检查是否允许使用位置服务。此代码来自苹果的“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基站 如果您在模拟器上测试,它将无法工作
之所以会发生这种情况,是因为只有当设备更改位置时才会调用该方法(并且模拟器从不更改位置)。请确保已将CLLocationManager添加为属性
@属性(非原子,强)CLLocationManager*locationManager 此外,在iOS8中,您必须有两件额外的东西:
- 向您的Info.plist添加一个密钥,并请求location manager授权,要求其启动
- NSLocationWhenUse用途说明
- N位置始终使用说明
- 您需要为相应的位置方法请求授权
- [self.locationManager在未使用授权时请求]
- [self.locationManager请求始终授权]
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];
来源:否。我不是在模拟器上检查。我是在iphone设备上检查。您是否尝试手动呼叫当前位置并确保您拥有有效的位置?您可以使用locationManager.location执行此操作。这不是真的,您现在可以模拟移动<代码>模拟器->调试->定位最终它成功了…我什么都没做。我只是做了一个新项目并复制了其中的所有类。我不知道怎么做?但它成功了…通常不鼓励使用Always方法,如果您不确定自己在做什么,请使用其他方法。Gabriel您已经向我展示了好方法。我现在得到了解决方案。我尝试调用didupdateLocation方法,但它没有调用,即使我应用了除您的建议以外的所有方法。最后,我应用了CLLocationManager作为属性。现在调用了委托方法,我得到了更新位置(横向、纵向)价值。非常感谢兄弟们。请告诉我有关property CLLocationManager的原因或解释,以及为什么没有property就无法工作?因为如果它是一个property,它有很强的参考价值。如果不是属性,它将在方法外部解除分配。因此,它无法接收来自委托的回调@用户3182143谢谢你,加布里埃尔,马萨纳