iOS: Watch Kit get current location and show on map
Try the following code:
@property (nonatomic, strong) CLLocationManager *locationManager;
- (void)willActivate
{
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager requestLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
if ([locations count] == 0) {
// error
return;
}
// success
self.currentLocation = [locations firstObject];
CLLocationCoordinate2D myLoc = CLLocationCoordinate2DMake(
self.currentLocation.coordinate.latitude,
self.currentLocation.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(nonnull NSError *)error
{
// error
}
Retrieving location on the Apple Watch
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.
Watchkit Maps: show current location
If you have the coordinates for the location you can use - (void)addAnnotation (CLLocationCoordinate2D)location withImage:(UIImage *)image centerOffset:(CGPoint)offset
on WKInterfaceMap. Pass in the location for the first parameter and pass in a UIImage for the pin that you want to show for the current location. You will also want to be sure and call setVisibleMapRect
so that the map shows the location.
WatchKit and Geolocation
To take the current user location, you should follow this step:
- Add CoreLocation framework to libraries off IOS app.
- In supporting file folder of IOS app, and info.plist file, you should add two row with name :
NSLocationWhenInUseDescription and value you can set "To know your locaton!!"
and another row:
NSLocationAlwaysUsageDescription and value you can set "Always using location!"
- Now import CoreLocation to view controller.swift and add a delegate of CLLocationDelegate.
Now in watchkit extension, you should import CoreLocation and CLLocationManager Delegate too and follow this step in awakeWithContext :
//Make a new locationManager variable:
var locationManager = CLLocationManager()
// set delegate
locationManager.delegate = self
// Set desiredAccuracy using a GPS of IP:
locationManager.desiredAccuracy = kCLLocationAccuracyBest
// request to use location
locationManager.requestAlwaysAuthorization()
// start update location
locationManager.startUpdatingLocation()
after that you write the CLLocationManagerDelegate:
// MARK: CLLocationManagerDelegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println(locations)
}
Note that you have run your IOS app first to accept to take a location of the user, after that you can run Watchkit scheme to take the current User Location.
Using Core Location in Apple WatchKit
In order to do what you want you don't need CoreLocation you just need a magnetometer.
AFAIK the Apple Watch doesn't have one built in.
The sensors it has are...
- Accelerometer
- Gyroscope
- Heart rate sensor
- Barometer
No magnetometer though.
Related Topics
Drawing a Polygon with One Color for Stroke, and a Different One for Fill
Segues Initiated Directly from View Controllers Warning in Storyboard Xcode
How to Search for Any Uiwebview Component Usage Inside a Current Project
Allow Full Access Check in Keyboards iOS10
Animating Calayer's Shadowpath Property
Why Is a Https Nsurlsession Connection Only Challenged Once Per Domain
Why Self.Locationmanager Stopupdatinglocation Doesn't Stop Location Update
Private VS. Fileprivate on Declaring Global Variables/Consts in Swift3
Supportedinterfaceorientations Not Working
How to Get MAC Address from Cbperipheral and Cbcenter
Preventing Avcapturevideopreviewlayer from Rotating, But Allow UI Layer to Rotate with Orientation
How to Hide a Bar Button Item for Certain Users
iOS Ibeacon: How to Get All of Proximityuuid Programmatically