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

时间:2023-02-05 20:44:50

I need to fetch the user current location for the WatchKit App in watchOS 2. How do I do this?

我需要获取watchOS 2中的WatchKit应用程序的用户当前位置。我该怎么做呢?

1 个解决方案

#1


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.

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

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.

然后,您将在以下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
}

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

参见WWDC15视频“核心位置的新功能”——https://developer.apple.com/videos/wwdc5/? id=714

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

#1


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.

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

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.

然后,您将在以下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
}

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

参见WWDC15视频“核心位置的新功能”——https://developer.apple.com/videos/wwdc5/? id=714

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