【问题标题】:how to call didUpdateToLocation after shake on iPhone在 iPhone 上摇晃后如何调用 didUpdateToLocation
【发布时间】:2022-04-10 02:44:56
【问题描述】:

我正在尝试在用户摇动他的 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];
} 

【问题讨论】:

    标签: ios core-location


    【解决方案1】:

    确保已启用位置服务并且应用可以访问位置服务(在 iPhone 设置 -> 位置服务下)。您使用的方法自 iOS 6 起已弃用。您应该使用

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

    我建议您也实现以下方法以用于调试目的。

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

    ps。我不确定您的方法 first 中 locationManager 的范围。你为什么不让它(强)属性,然后在 viewDidLoad 中初始化它,并且只在你的方法 first 中调用 startUpdatingLocation

    【讨论】:

    • 感谢您的回复,我需要在摇动后获取纬度和经度值。所以请告诉我如何在摇动模拟器或移动设备时获取纬度和经度值
    猜你喜欢
    • 2011-10-24
    • 2010-09-14
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多