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

时间:2015-07-28 07:11:54

标签: objective-c location watchkit apple-watch watch-os-2

我需要在watchOS 2中获取WatchKit App的用户当前位置。我该怎么办?

1 个答案:

答案 0 :(得分:23)

The other answers are incorrect. You can request location directly on the watch for watch OS2. The available method is called requestLocation. It allows you to request a single location update.

E.g.

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

Then where you want to request location:

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

Then you will get a single callback in one of the following CLLocationManagerDelegate methods.

- (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
}

See the WWDC15 video "What's New in Core Location" - https://developer.apple.com/videos/wwdc/2015/?id=714

Imgur