【发布时间】:2016-01-09 18:21:52
【问题描述】:
我想获取用户的经纬度并显示在 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
【问题讨论】:
-
你有什么问题?
-
您实际上无法找到手表的位置,因为它没有 GPS。您会找到配对手机的位置。您使用标准 CLLocationManager 代码来执行此操作。
-
@Paulw11 但我使用的是
CLLocationManager -
我在这里没有看到任何必要的方法——请求位置权限、创建位置管理器、设置代理
标签: ios objective-c watchkit apple-watch