如何在 WatchKit 扩展中计算当前位置 [英] How to calculate current location in WatchKit extension

查看:15
本文介绍了如何在 WatchKit 扩展中计算当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Watch Kit 扩展中计算当前用户位置,因为我们不能在 Watch Kit 中使用 CoreLocation.

How to calculate the current user location in Watch Kit extension as we can't use CoreLocation in watch kit.

提前致谢

推荐答案

您可以在手表应用扩展程序中使用 CoreLocation,与在 iPhone 应用中使用 CoreLocation 的方式非常相似.关键区别在于用户无法授权您的扩展程序访问核心位置.他们需要从您的 iPhone 应用程序中执行此操作.因此,您需要检查用户是否为您的应用授权了位置服务,如果没有,您需要指导他们如何操作.

You can use CoreLocation in your watch app extension very similarly to how you use it in your iPhone app. The key difference is that a user can't authorize your extension to have access to Core Location. They will need to do that from your iPhone app. So you will need to check if the user has authorized location services for your app and if they haven't, you will need to instruct them how to do it.

这是我在手表套件扩展中使用的代码,用于跟踪当前位置.(GPWatchAlertView 是我用来显示警报消息的自定义控制器.)

Here is the code I use in my watch kit extension for tracking the current location. (GPWatchAlertView is a custom controller I made to show alert messages.)

#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];

   ... 

}

这篇关于如何在 WatchKit 扩展中计算当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆