简体   繁体   English

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

[英]iOS: Watch Kit get current location and show on map

I have an iOS app that uses a users current location to show directions/ distance between the users current location and a destination point. 我有一个iOS应用,该应用使用用户当前位置来显示用户当前位置与目的地之间的方向/距离。 I am able to show the destination point on the map, however when I try to get the user's current location using CLlocationManager, it returns coordinates (0,0). 我能够在地图上显示目的地,但是当我尝试使用CLlocationManager获取用户的当前位置时,它将返回坐标(0,0)。 Below is my code for retrieving the users location. 以下是我用于检索用户位置的代码。

Is retrieving current location possible on the Apple Watch? 是否可以在Apple Watch上检索当前位置?

If so, what am I doing wrong here that the location is returning (0,0)? 如果是这样,我在这里错了,该位置正在返回(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);

Also, I have #import < CoreLocation/CoreLocation.h> in my .h file and < CLLocationManagerDelegate> 另外,我的.h文件中有#import <CoreLocation / CoreLocation.h>和<CLLocationManagerDelegate>

Try the following code: 尝试以下代码:

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

If you did not implemented this: (with attributes inspector or programmatically) 如果您没有实现此功能:(使用属性检查器或以编程方式)

self.mapView.showsUserLocation = YES self.mapView.showsUserLocation =是

Or in addition to tell the mapView that the user's location is depending of LocationManager.location 或另外告诉mapView用户的位置取决于LocationManager.location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM