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

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

问题描述

如何计算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中使用它的方式非常相似应用程序。关键区别在于用户无法授权您的扩展程序可以访问Core Location。他们需要从你的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天全站免登陆