Getting locations on Apple Watch

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

I have already included the basic layout structure in the Watchkit extension.

When I run the program, all I get is 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?

.H file:

#import <WatchKit/WatchKit.h> #import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> @interface InterfaceController : WKInterfaceController @end 

.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 
+2
source share
5 answers

In Xcode, you want to use the Debug menu to simulate a location that is either predefined or uses the GPX file as the location source.

+1
source

Before you can receive any location updates in your application to view the watch, you will need to enable location updates in your iPhone application. If you do not have authorized location updates in your iPhone application, your chat extension will not receive any location updates. Also, I'm sure you need to set permission to always allow location updates [CLLocationManager requestAlwaysAuthorization] . I do not think this will work if you use [CLLocationManager requestWhenInUseAuthorization] , although I am not 100% sure about permissions.

+3
source

In the CLLocationMananager location documentation contains

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

This means that you need to call [self.locationManager startUpdatingLocation] to get the correct location. But there are things you need to do before it works.


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

When permission is approved or denied, the delegate method is called locationManager:didChangeAuthorizationStatus:

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

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

+1
source

You do not have to request location data as part of the WatchKit extension.

From the Apple Watch Human Interface Guide :

โ€œAvoid using technologies that ask for user permission, such as Core Location. Using your WatchKit extensionโ€™s technology may turn on unexpected iPhone invitations to appear to users first when you make a request. Worse, it may happen while iPhone is in userโ€™s pocket and not displayed. "

+1
source

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 for 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))]; 
0
source

Source: https://habr.com/ru/post/981038/


All Articles