Retrieving location on the Apple Watch

2019-07-09 09:16发布

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

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

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

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?

The .h file:

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

@interface InterfaceController : WKInterfaceController

@end

The .m file:

#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

5条回答
手持菜刀,她持情操
2楼-- · 2019-07-09 09:46

In the CLLocationMananager documentation, the location property states

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

That means that you need to call [self.locationManager startUpdatingLocation] in order to get a valid location. But there a could things you need to do before that will work.


First of all you will need to request authorization by calling [self.locationManager requestAlwaysAuthorization].

When authorization is approved or declined, the delegate method locationManager:didChangeAuthorizationStatus: will be called.

If the authorization status is kCLAuthorizationStatusAuthorizedAlways than you can call [self.locationManager startUpdatingLocation].

Then anytime the location is updated, the delegate method locationManager:didUpdateLocations: will be called so you can update your UI with the new location.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-09 09:49

You are not supposed to request location data within a WatchKit Extension.

From Apple Watch Human Interface Guidelines:

“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.”

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-07-09 09:53

I am using this code

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

}

[locationManager startUpdatingLocation];

and to display in wkinterfaceMap i use this code

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

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


[self.mapkit setRegion:(MKCoordinateRegionMake(mapLocation, coordinateSpan))];
查看更多
淡お忘
5楼-- · 2019-07-09 09:54

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.

查看更多
我命由我不由天
6楼-- · 2019-07-09 09:55

Before you can get any location updates to your watch app extension you will need to authorize location updates in your iPhone app. If you haven't authorized location updates in your iPhone app, then your watch extension will not get any location updates. Also, I am pretty sure you need to set the permission to always allow for location updates [CLLocationManager requestAlwaysAuthorization]. I don't think it will work if you use [CLLocationManager requestWhenInUseAuthorization], though I am not 100% sure about the permissions.

查看更多
登录 后发表回答