在 Apple Watch 上检索位置

Retrieving location on the Apple Watch

我想获取用户的经纬度,显示在Apple Watch上。

我已经在我的 Watchkit 扩展中包含了核心定位框架。

当我 运行 程序时,我得到的所有经纬度都是 0.0 和 0.0

我在 iPhone 上的 class 中测试了相同的方法并且有效,并给了我适当的坐标。我究竟做错了什么?

.h 文件:

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface InterfaceController : WKInterfaceController

@end

.m 文件:

#import "InterfaceController.h"

@interface InterfaceController()

@end

@implementation InterfaceController

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    // Configure interface objects here.
}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }

    [self.locationManager startUpdatingLocation];
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
}

- (IBAction)showLocation {
    NSString * geoLoc  = [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
    NSLog(geoLoc);
}

@end

在 Xcode 中,您希望使用 调试 菜单来模拟预先设置或使用 GPX 文件作为位置源的位置。

在您可以为您的手表应用程序扩展程序获取任何位置更新之前,您需要在您的 iPhone 应用程序中授权位置更新。如果您尚未在 iPhone 应用中授权位置更新,那么您的手表扩展将不会获得任何位置更新。此外,我很确定您需要将权限设置为始终允许位置更新 [CLLocationManager requestAlwaysAuthorization]。我不认为如果你使用 [CLLocationManager requestWhenInUseAuthorization] 它会起作用,虽然我不是 100% 确定权限。

CLLocationMananager 文档中,location 属性 声明

The value of this property is nil if no location data has ever been retrieved.

这意味着您需要调用 [self.locationManager startUpdatingLocation] 才能获得有效位置。但是在这之前您可能需要做一些事情。


首先,您需要通过调用 [self.locationManager requestAlwaysAuthorization] 请求授权。

当授权被批准或拒绝时,将调用委托方法locationManager:didChangeAuthorizationStatus:

如果授权状态为 kCLAuthorizationStatusAuthorizedAlways,则可以调用 [self.locationManager startUpdatingLocation]

然后只要更新位置,委托方法 locationManager:didUpdateLocations: 将被调用,因此您可以使用新位置更新您的 UI。

您不应该在 WatchKit 扩展中请求位置数据。

来自 Apple Watch 人机界面指南

“Avoid using technologies that request user permission, like Core Location. Using the technology from your WatchKit extension could involve displaying an unexpected prompt on the user’s iPhone the first time you make the request. Worse, it could happen at a time when the iPhone is in the user’s pocket and not visible.”

我正在使用此代码

 locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
{
    [locationManager requestAlwaysAuthorization];
    [locationManager requestWhenInUseAuthorization];

}

[locationManager startUpdatingLocation];

为了在 wkinterfaceMap 中显示,我使用了这段代码

 CLLocationCoordinate2D mapLocation = CLLocationCoordinate2DMake([latitude floatValue],[longitude floatValue]);
//
    MKCoordinateSpan coordinateSpan = MKCoordinateSpanMake(1, 1);

    [self.mapkit addAnnotation:mapLocation withPinColor: WKInterfaceMapPinColorPurple];


[self.mapkit setRegion:(MKCoordinateRegionMake(mapLocation, coordinateSpan))];