如何在iPhone上摇动后调用didUpdateToLocation

时间:2014-09-25 11:43:40

标签: ios core-location

我正在尝试在用户摇动iPhone时获取位置更新,我在接收震动事件时启用了GPS,但我的问题是没有调用 didUpdateToLocation 方法。有人能告诉我我的代码有什么问题吗?

    - (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];
}

所以请给我任何想法。

感谢高级。

1 个答案:

答案 0 :(得分:0)

确保已启用位置服务,并且App可以访问位置服务(在iPhone设置 - >位置服务下)。从iOS 6开始,不推荐使用您使用的方法。您应该使用

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

我建议您实现以下方法以进行调试。

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

PS。我不确定方法第一个中locationManager的范围。为什么不制作(强)属性,然后在 viewDidLoad 中初始化它,并且只在方法首先中调用 startUpdatingLocation