【问题标题】:Retrieving location on the Apple Watch在 Apple Watch 上检索位置
【发布时间】:2016-01-09 18:21:52
【问题描述】:

我想获取用户的经纬度并显示在 Apple Watch 上。

我已经在我的 Watchkit Extension 中包含了核心位置框架。

当我运行程序时,我得到的 lat 和 long 都是 0.0 和 0.0

我在 iPhone 上的一个类中测试了相同的方法,它有效,并给了我适当的坐标。我究竟做错了什么?

.h 文件:

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface InterfaceController : WKInterfaceController

@end

.m 文件:

#import "InterfaceController.h"

@interface InterfaceController()

@end

@implementation InterfaceController

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    // Configure interface objects here.
}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }

    [self.locationManager startUpdatingLocation];
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
}

- (IBAction)showLocation {
    NSString * geoLoc  = [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
    NSLog(geoLoc);
}

@end

【问题讨论】:

  • 你有什么问题?
  • 您实际上无法找到手表的位置,因为它没有 GPS。您会找到配对手机的位置。您使用标准 CLLocationManager 代码来执行此操作。
  • @Paulw11 但我使用的是CLLocationManager
  • 我在这里没有看到任何必要的方法——请求位置权限、创建位置管理器、设置代理

标签: ios objective-c watchkit apple-watch


【解决方案1】:

在您可以获取任何位置更新到您的手表应用扩展程序之前,您需要在您的 iPhone 应用中授权位置更新。如果您没有在 iPhone 应用程序中授权位置更新,那么您的手表扩展程序将不会获得任何位置更新。另外,我很确定您需要将权限设置为始终允许位置更新[CLLocationManager requestAlwaysAuthorization]。我认为如果您使用[CLLocationManager requestWhenInUseAuthorization],它不会起作用,尽管我不能 100% 确定权限。

【讨论】:

  • 这在早期的测试版中是正确的,但在发布版本中使用时按预期工作。
【解决方案2】:

在 Xcode 中,您希望使用 Debug 菜单来模拟预先设置的位置或使用 GPX 文件作为位置源。

【讨论】:

  • 我实际上是使用“Apple”选项进行的,但它仍然不起作用。
【解决方案3】:

CLLocationMananager 文档中,location 属性状态

如果从未检索到任何位置数据,则此属性的值为 nil

这意味着您需要致电[self.locationManager startUpdatingLocation] 才能获得有效位置。但是在此之前您需要做一些事情。


首先,您需要致电[self.locationManager requestAlwaysAuthorization] 请求授权。

当授权被批准或拒绝时,将调用委托方法locationManager:didChangeAuthorizationStatus:

如果授权状态为kCLAuthorizationStatusAuthorizedAlways,则可以调用[self.locationManager startUpdatingLocation]

然后,无论何时更新位置,委托方法 locationManager:didUpdateLocations: 将被调用,以便您可以使用新位置更新您的 UI。

【讨论】:

    【解决方案4】:

    您不应该在 WatchKit 扩展中请求位置数据。

    来自 Apple Watch 人机界面指南

    “避免使用请求用户许可的技术,例如 Core 地点。使用 WatchKit 扩展中的技术可以 涉及首先在用户的 iPhone 上显示意外提示 您提出请求的时间。更糟糕的是,它可能发生在 iPhone 在用户的口袋里,不可见。”

    【讨论】:

      【解决方案5】:

      我正在使用此代码

       locationManager = [[CLLocationManager alloc] init];
      locationManager.delegate = self;
      [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
      if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
      {
          [locationManager requestAlwaysAuthorization];
          [locationManager requestWhenInUseAuthorization];
      
      }
      
      [locationManager startUpdatingLocation];
      

      并在 wkinterfaceMap 中显示我使用此代码

       CLLocationCoordinate2D mapLocation = CLLocationCoordinate2DMake([latitude floatValue],[longitude floatValue]);
      //
          MKCoordinateSpan coordinateSpan = MKCoordinateSpanMake(1, 1);
      
          [self.mapkit addAnnotation:mapLocation withPinColor: WKInterfaceMapPinColorPurple];
      
      
      [self.mapkit setRegion:(MKCoordinateRegionMake(mapLocation, coordinateSpan))];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-08
        • 2017-12-04
        • 1970-01-01
        • 2018-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多