如何在watchOS 2中获取当前位置?

20
我需要为WatchOS 2的WatchKit应用获取用户当前位置。我该怎么做?

3
为什么这个问题会被踩?它是一个完全合理的问题。SMH - Rickster
1个回答

24

其他答案都是不正确的。在watchOS 2上,您可以直接在手表上请求位置。可用的方法称为requestLocation。它允许您请求单个位置更新。

例如:

#import <CoreLocation/CoreLocation.h>
@interface className : WKInterfaceController<CLLocationManagerDelegate>

那么您希望请求位置的地方是哪里:

self.locationManager = [CLLocationManager new];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestLocation];

然后您将在以下CLLocationManagerDelegate方法之一中获得单个回调。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    // Callback here with single location if location succeeds
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    // Error here if no location can be found
}

查看WWDC15视频“Core Location的新功能” - https://developer.apple.com/videos/wwdc/2015/?id=714

Imgur


1
谢谢!但是重要的是要提到这在ExtensionDelegate中不起作用。在“WKInterfaceController”中,它可以完美地运行。 - Blank
@Blankarsch 这是有什么原因吗?如果我们需要一个独立于 UI 的中央管理器呢? - TealShift

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接