How to calculate your current location in the WatchKit extension

How to calculate the current location of a user in an extension of a watch set, since we cannot use CoreLocation in a watch set.

Thank you in advance

+6
source share
3 answers

You can use CoreLocation in the extension of your watch application just as you use it in your iPhone application. The main difference is that the user cannot allow your extension to have access to Core Location. They will need to do this from your iPhone application. Therefore, you will need to check whether the user has authorized location services for your application, and if not, you will need to instruct them on how to do this.

Here is the code I'm using in my watch extension to track my current location. ( GPWatchAlertView is a custom controller that I made to display warning messages.)

 #pragma mark - CLLocation Manager -(void)startTrackingCurrentLocation:(BOOL)forTrip { if (self.locationManager == nil) { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; self.locationManager.activityType = CLActivityTypeFitness; self.locationManager.distanceFilter = 5; //Require 15 meters of movement before we show an update } CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) { NSLog(@"%@ Start tracking current location", self); self.trackingCurrentLocation = YES; self.gpsTrackingForTrip = forTrip; //We wait until we have a GPS point before we start showing it self.showCurrentLocation = NO; [self.locationManager startUpdatingLocation]; } else { [self presentControllerWithName:@"GPWatchAlertView" context:@"Unauthorized GPS Access. Please open Topo Maps+ on your iPhone and tap on current location."]; } } -(void)stopTrackingCurrentLocation:(id)sender { NSLog(@"%@ Stop tracking current location", self); self.trackingCurrentLocation = NO; [self.locationManager stopUpdatingLocation]; self.showCurrentLocation = NO; } -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation* loc = [locations lastObject]; ... } 
+9
source

Stephan's answer should work (not tested it) with one exception. WatchKit requires Always permission to manage your location. This is due to the fact that your phone really works with the extension of the clock in the background. Therefore, if you only request permission "When you use", you will never receive addresses that will be returned to your watch extension.

Try changing the line:

 if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) 

with:

 if (status == kCLAuthorizationStatusAuthorizedAlways) 
+2
source

you must get the user's location in the iphone application not in the extension. Please check apple documentation .

+1
source

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


All Articles