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
In the
CLLocationMananager
documentation, thelocation
property statesThat 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.You are not supposed to request location data within a WatchKit Extension.
From Apple Watch Human Interface Guidelines:
I am using this code
and to display in wkinterfaceMap i use this code
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.
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.