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 );
        }];
    }
}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐