检索Apple Watch上的位置 [英] Retrieving location on the Apple Watch

查看:130
本文介绍了检索Apple Watch上的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get the latitude and longitude of the user and display it on the Apple Watch.

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

I have already included the core location framework in my Watchkit Extension.

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

When I run the program all I get for the lat and long is 0.0 and 0.0

我在iPhone的一个类中测试了相同的方法,并且该方法有效,并为我提供了适当的坐标.我究竟做错了什么?

I tested the same method in a class on the iPhone and it worked, and gave me the appropriate coordinates. What am I doing wrong?

.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中,您想使用 Debug 菜单来模拟一个预先设置的位置或使用GPX文件作为该位置的位置来源.

In Xcode, you want use the Debug menu to simulate a location that's either pre-set or use a GPX file as the location source.

这篇关于检索Apple Watch上的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆