由于我们不能在 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/