ios - 如何计算 WatchKit 扩展中的当前位置

标签 ios watchkit apple-watch

由于我们不能在 watch Kit 中使用 CoreLocation,因此如何在 Watch Kit 扩展中计算当前用户位置。

提前致谢

最佳答案

您可以在 watch 应用程序扩展中使用 CoreLocation,就像在 iPhone 应用程序中使用它一样。关键区别在于用户无法授权您的扩展程序访问 Core Location。他们将需要通过您的 iPhone 应用程序执行此操作。因此,您需要检查用户是否为您的应用授权了位置服务,如果没有,您需要指导他们如何操作。

这是我在 watch 套件扩展中用于跟踪当前位置的代码。 (GPWatchAlertView 是我制作的用于显示警报消息的自定义 Controller 。)

#pragma mark - CLLocation Manager 

-(void)startTrackingCurrentLocation:(BOOL)forTrip
{
    if (self.locationManager == nil)
    {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        self.locationManager.activityType = CLActivityTypeFitness;
        self.locationManager.distanceFilter = 5; //Require 15 meters of movement before we show an update
    }

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse)
    {
        NSLog(@"%@ Start tracking current location", self);

        self.trackingCurrentLocation = YES;
        self.gpsTrackingForTrip = forTrip;

        //We wait until we have a GPS point before we start showing it
        self.showCurrentLocation = NO;
        [self.locationManager startUpdatingLocation];
    }
    else
    {
        [self presentControllerWithName:@"GPWatchAlertView" context:@"Unauthorized GPS Access.  Please open Topo Maps+ on your iPhone and tap on current location."];
    }

}

-(void)stopTrackingCurrentLocation:(id)sender
{
    NSLog(@"%@ Stop tracking current location", self);

    self.trackingCurrentLocation = NO;
    [self.locationManager stopUpdatingLocation];
    self.showCurrentLocation = NO;
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation* loc = [locations lastObject];

   ... 

}

关于ios - 如何计算 WatchKit 扩展中的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27960108/

相关文章:

ios - 我们可以在Apple Watch上使用网络服务吗?

swift - 如何在 watchOS 上像 TextView 一样显示文本

ios - 如何检查 iPhone 和 Apple Watch 是否连接

iphone - 设置后对象保持为空

ios - 将自定义 URL 方案与 UIWebView 结合使用

iphone - 如何从 iPhone 中的 UIWebView 获取触摸位置经纬度值?

javascript - iOS 视频的文件输入,当屏幕不再事件时视频压缩终止

swift - 没有这样的模块错误 "NotificationCenter"WatchOS,Swift

swift - WatchKit 应用程序中的多个 Storyboard

ios - 如何将 Apple Pay 集成到 watch 应用程序中?