How to get the current location in watchOS 2?

前端 未结 1 498
走了就别回头了
走了就别回头了 2020-12-15 05:23

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

相关标签:
1条回答
  • 2020-12-15 06:08

    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

    0 讨论(0)
提交回复
热议问题