如何在 Apple Watch 中集成苹果地图?
How to integrate apple map in Apple Watch?
我是 Apple Watch 开发的新手,想在 Apple Watch 中集成苹果地图,它只想显示我的当前位置,但每次我在模拟器中加载界面时,我都会得到如下所示的屏幕。任何帮助都将不胜感激。
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* loc = [locations lastObject];
CLLocationCoordinate2D myLocationCoordinate2D = loc.coordinate;
[self.demoMap setRegion:MKCoordinateRegionMake(myLocationCoordinate2D, MKCoordinateSpanMake(0.0,0.0))];//set map region
[self.demoMap addAnnotation:myLocationCoordinate2D withPinColor:WKInterfaceMapPinColorGreen];// set pin on map
[locationManager stopUpdatingLocation];
}
#pragma mark - CLLocation Manager
-(void)startTrackingCurrentLcoation:(BOOL)forTrip
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestAlwaysAuthorization];
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse)
{
NSLog(@"%@ Start tracking current location", self);
[locationManager startUpdatingLocation];
}
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusDenied)
{
NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";
NSLog(@"%@",message);
}
else if (status == kCLAuthorizationStatusNotDetermined)
{
[locationManager requestAlwaysAuthorization];
}
else
{
[locationManager startUpdatingLocation];
}
}
我自己试了一下,里面有很多陷阱。
我破解了一个演示项目,您可以在这里找到它:
https://github.com/sticksen/WatchKit-Map-Sample-Project
清单:
- 务必请求定位服务授权并在用户授予访问权限后开始更新位置。
- 如果已获得授权,您可以立即开始更新位置
现在最糟糕的部分是:
- 您的主要目标(不是 WatchKit 目标,只是为了说明清楚!)Info.plist 必须有一个名称为
NSLocationWhenInUseUsageDescription
或 NSLocationAlwaysUsageDescription
的密钥(取决于您请求的授权)作为值有一个描述字符串,说明您为什么要使用定位服务。如果不在 Info.plist 中设置此键,它将无法工作 !
关注此 link https://trymakedo.wordpress.com/tag/wkinterfacemap/.
通过给定预定义的坐标,检查您获取传递给 region.Test 的当前位置坐标的天气情况。
我是 Apple Watch 开发的新手,想在 Apple Watch 中集成苹果地图,它只想显示我的当前位置,但每次我在模拟器中加载界面时,我都会得到如下所示的屏幕。任何帮助都将不胜感激。
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* loc = [locations lastObject];
CLLocationCoordinate2D myLocationCoordinate2D = loc.coordinate;
[self.demoMap setRegion:MKCoordinateRegionMake(myLocationCoordinate2D, MKCoordinateSpanMake(0.0,0.0))];//set map region
[self.demoMap addAnnotation:myLocationCoordinate2D withPinColor:WKInterfaceMapPinColorGreen];// set pin on map
[locationManager stopUpdatingLocation];
}
#pragma mark - CLLocation Manager
-(void)startTrackingCurrentLcoation:(BOOL)forTrip
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestAlwaysAuthorization];
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse)
{
NSLog(@"%@ Start tracking current location", self);
[locationManager startUpdatingLocation];
}
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusDenied)
{
NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";
NSLog(@"%@",message);
}
else if (status == kCLAuthorizationStatusNotDetermined)
{
[locationManager requestAlwaysAuthorization];
}
else
{
[locationManager startUpdatingLocation];
}
}
我自己试了一下,里面有很多陷阱。
我破解了一个演示项目,您可以在这里找到它: https://github.com/sticksen/WatchKit-Map-Sample-Project
清单:
- 务必请求定位服务授权并在用户授予访问权限后开始更新位置。
- 如果已获得授权,您可以立即开始更新位置
现在最糟糕的部分是:
- 您的主要目标(不是 WatchKit 目标,只是为了说明清楚!)Info.plist 必须有一个名称为
NSLocationWhenInUseUsageDescription
或NSLocationAlwaysUsageDescription
的密钥(取决于您请求的授权)作为值有一个描述字符串,说明您为什么要使用定位服务。如果不在 Info.plist 中设置此键,它将无法工作 !
关注此 link https://trymakedo.wordpress.com/tag/wkinterfacemap/.
通过给定预定义的坐标,检查您获取传递给 region.Test 的当前位置坐标的天气情况。