7

How to calculate the current user location in Watch Kit extension as we can't use CoreLocation in watch kit.

Thanks in advance

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Mohit Totlani
  • 825
  • 1
  • 10
  • 21

3 Answers3

10

You can use CoreLocation in your watch app extension very similarly to how you use it in your iPhone app. The key difference is that a user can't authorize your extension to have access to Core Location. They will need to do that from your iPhone app. So you will need to check if the user has authorized location services for your app and if they haven't, you will need to instruct them how to do it.

Here is the code I use in my watch kit extension for tracking the current location. (GPWatchAlertView is a custom controller I made to show alert 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];

   ... 

}
Brad The App Guy
  • 16,255
  • 2
  • 41
  • 60
Stephen Johnson
  • 5,293
  • 1
  • 23
  • 37
  • And how can we test? that if this code is correct or not. Because neither Apple watch is available nor we can set location for apple watch simulator – Mrugesh Tank Feb 18 '15 at 10:22
  • In the simulator you can use the Debug->Location to simulate locations. See http://stackoverflow.com/questions/28079454/retrieving-location-on-the-apple-watch for more info. – Stephen Johnson Feb 18 '15 at 13:31
  • 1
    AFAIK it is only for phone simulator.Is it also work for watch simulator? – Mrugesh Tank Feb 18 '15 at 13:33
  • 1
    Yes, it also works for the watch extension. There is no GPS on the watch, it is just on the phone. Your watch extension is set up to get GPS events and so it is actually the phone that is receiving the events, and not the watch. – Stephen Johnson Feb 18 '15 at 13:36
  • @StephenJohnson i have used your code but still i don't get the location in iWatch – Chirag Shah Apr 03 '15 at 12:50
  • I found that I usually I had to use the Menu->Debug->Location->Custom Location in the simulator to get location events in the extension. Sometimes the location for my debug scheme would work, but usually it it didn't. – Stephen Johnson Apr 06 '15 at 13:12
3

The answer by Stephan should work (haven't tested it), with just one exception. WatchKit requires "Always" permission for the location manages. This is because your phone is really running the watch extension, in background mode. So if you only ask for "When in Use" permission, you will never get locations returned to your watch extension.

Try changing the line:

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

with:

if (status == kCLAuthorizationStatusAuthorizedAlways)
rfornal
  • 5,072
  • 5
  • 30
  • 42
rmp
  • 3,503
  • 1
  • 17
  • 26
1

you should get user location on iphone app not in extension. Please check apple documentation.

Iqbal Khan
  • 4,587
  • 8
  • 44
  • 83