iOS:Watch Kit获取当前位置并在地图上显示

MSU*_*dog 1 objective-c ios watchkit

我有一个iOS应用,该应用使用用户当前位置来显示用户当前位置与目的地之间的方向/距离。我可以在地图上显示目的地,但是当我尝试使用CLlocationManager获取用户的当前位置时,它将返回坐标(0,0)。以下是我用于检索用户位置的代码。

是否可以在Apple Watch上检索当前位置?

如果是这样,我在这里错了,该位置正在返回(0,0)?

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = 5;
CLLocation *location = locationManager.location;

CLLocationCoordinate2D myLoc = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
Run Code Online (Sandbox Code Playgroud)

另外,我的.h文件中有#import <CoreLocation / CoreLocation.h>和<CLLocationManagerDelegate>

Kos*_*awa 5

尝试以下代码:

@property (nonatomic, strong) CLLocationManager *locationManager;

- (void)willActivate
{
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager setDelegate:self];
    [self.locationManager requestLocation];
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    if ([locations count] == 0) {
        // error
        return;
    }

    // success
    self.currentLocation = [locations firstObject];
    CLLocationCoordinate2D myLoc = CLLocationCoordinate2DMake(
        self.currentLocation.coordinate.latitude, 
        self.currentLocation.coordinate.longitude);
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(nonnull NSError *)error
{
    // error
}
Run Code Online (Sandbox Code Playgroud)