2

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

我已经在我的 Watchkit Extension 中包含了核心位置框架。

当我运行程序时,我得到的 lat 和 long 是 0.0 和 0.0

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

.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
4

5 回答 5

3

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

于 2015-01-22T15:43:35.347 回答
1

CLLocationMananager文档中,location属性状态

此属性的值是nil如果没有检索到任何位置数据。

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


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

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

如果授权状态kCLAuthorizationStatusAuthorizedAlways比你可以调用[self.locationManager startUpdatingLocation]

然后,无论何时更新位置, locationManager:didUpdateLocations:都会调用委托方法,以便您可以使用新位置更新 UI。

于 2015-01-22T16:27:01.520 回答
1

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

于 2015-01-22T03:57:27.970 回答
1

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

来自Apple Watch 人机界面指南

“避免使用请求用户许可的技术,例如核心位置。使用 WatchKit 扩展中的技术可能会在您第一次发出请求时在用户的 iPhone 上显示意外提示。更糟糕的是,它可能发生在 iPhone 放在用户口袋中且不可见的时候。”</p>

于 2015-05-02T06:10:36.473 回答
0

我正在使用此代码

 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))];
于 2016-01-09T10:21:52.180 回答