Ios 在iPhone上震动后如何调用DidUpdateLocation
我尝试在用户摇晃iPhone时获取位置更新,我在接收摇晃事件时启用GPS,但我的问题是没有调用didUpdateToLocation方法。有人能告诉我我的代码有什么问题吗Ios 在iPhone上震动后如何调用DidUpdateLocation,ios,core-location,Ios,Core Location,我尝试在用户摇晃iPhone时获取位置更新,我在接收摇晃事件时启用GPS,但我的问题是没有调用didUpdateToLocation方法。有人能告诉我我的代码有什么问题吗 - (void)viewDidLoad { [super viewDidLoad]; } - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if(event.t
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.type == UIEventSubtypeMotionShake)
{
[self first];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
// NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
NSString * currentLat = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
NSLog(@"currentLat%@",currentLat);
NSString * currentLong = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
NSLog(@"currentLong%@",currentLong);
}
-(void)first
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(@"called");
[self.view setBackgroundColor:[UIColor greenColor]];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
所以请告诉我一些想法
提前谢谢 确保位置服务已启用,并且应用程序可以访问位置服务(在iPhone设置->位置服务下)。自iOS 6以来,您使用的方法已被弃用。你应该使用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
为了调试,我建议您也实现以下方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
另外,我不确定您的方法中locationManager的范围。你为什么不做(坚强)属性,然后在viewDidLoad中对其进行初始化,并仅在方法中调用startUpdatingLocation首先调用,,谢谢回复,我需要在抖动后获得纬度和经度值。因此,请告诉我如何在抖动模拟器或手机时获得纬度和经度值