【问题标题】:iPhone - Initial user location with CCLocationManageriPhone - 使用 CCLocationManager 的初始用户位置
【发布时间】:2011-11-07 04:36:46
【问题描述】:

我正在使用在 iPhone 4 上运行的代码:

- (id)init
{
    self = [super initWithNibName:@"OfflineView" bundle:nil]; // ok, not perfect but for test, that works fine
    if (self) {
        self.locationMgr = [[CLLocationManager alloc] init];
        self.locationMgr.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
        self.locationMgr.distanceFilter = kCLDistanceFilterNone;
        self.locationMgr.headingFilter = kCLHeadingFilterNone;

        self.locationMgr.delegate = self;
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    // do things
}

// triggered when showing the view, first call here after the init
- (void) start
{
    self.view.hidden = NO;
    [self.locationMgr startUpdatingLocation];
    [self.locationMgr startUpdatingHeading];
}

但是没有触发委托方法。
它仅在手机移动时触发。

如何在视图出现时使用有效的用户位置来启动我的流程,而不要求我的用户摇晃手机并跑 100 米,然后才能做某事?

【问题讨论】:

    标签: iphone cocoa-touch cllocationmanager userlocation


    【解决方案1】:

    您可以通过自己踢一次委托方法来“启动”它。

    - (void) start
    {
        self.view.hidden = NO;
        [self.locationMgr startUpdatingLocation];
        [self.locationMgr startUpdatingHeading];
        [self locationManager: self.locationMgr didUpdateToLocation: [self.locationMgr currentLocation] fromLocation: nil];
    }
    

    【讨论】:

      【解决方案2】:

      你在哪里调用开始:?你应该在不动的情况下得到第一个修复。这是一个异步回调,因此可能需要一些时间。

      理想情况下,您应该在 init/viewDidLoad 中调用 startUpdateLocation,然后在 locationUpdate 中读取它:

      - (void)locationUpdate:(CLLocation *)location {
      
       //   Read location
      
      }
      

      【讨论】:

        猜你喜欢
        • 2010-11-29
        • 1970-01-01
        • 2020-04-03
        • 1970-01-01
        • 1970-01-01
        • 2013-10-27
        • 1970-01-01
        • 1970-01-01
        • 2011-02-21
        相关资源
        最近更新 更多