1、加属性:
@property (nonatomic, strong) CLHeading *currentHeading; 方向
@property (nonatomic, strong) MKAnnotationView *mkAnnotationView; 地图图标
2、初始化:
if ([CLLocationManager headingAvailable]) {
_localManager.headingFilter = 1;
[_localManager startUpdatingHeading];
} else {
NSLog(@"不能获取方向===============");
}
3、替换图片:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *newAnnotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];
newAnnotation.image = [UIImage imageNamed:@"mapFlag.png"];
newAnnotation.canShowCallout = YES;
self.mkAnnotationView = newAnnotation;
return newAnnotation;
}
4、更新方向:
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
self.currentHeading = newHeading;
NSLog(@"朝向:%d度",(int)newHeading.trueHeading);
if (self.mkAnnotationView != nil) {
[UIView animateWithDuration:0.1 animations:^{
double degree = newHeading.trueHeading;
self.mkAnnotationView.transform = CGAffineTransformMakeRotation(degree * M_PI / 180.f );
}];
}
}