高德地图当前位置图标旋转功能实现

实现思路

1、添加高德地图
- (void)creatMapView {
    _mapView = [[MAMapView alloc]initWithFrame:self.view.bounds];
    _mapView.delegate = self;
    _mapView.showsCompass = NO;
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    //是否自定义用户位置精度圈
    _mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
    [self.view addSubview:_mapView];
}
2、实现地图添加大头针的代理,设置当前位置图标
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation {
    //用户当前位置大头针
    if ([annotation isKindOfClass:[MAUserLocation class]])
    {
        static NSString *kUserLocationStyleReuseIndetifier = @"userLocationStyleReuseIndetifier";
        
        MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:kUserLocationStyleReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kUserLocationStyleReuseIndetifier];
        }
        
        annotationView.canShowCallout = NO;
        annotationView.image = [UIImage imageNamed:@"heardImg_passenger_default"];
        annotationView.frame = CGRectMake(0, 0, 26, 26);
        annotationView.contentMode = UIViewContentModeScaleToFill;
        annotationView.layer.masksToBounds = YES;
        annotationView.tag = kUserHearderAnnotaionViewTag;
        
        return annotationView;
    }
    //其他大头针
    else if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        
        
    }
    return nil;
}
3、在地图时时位置更新的方法里实现旋转功能
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    NSLog(@"%f,%f,%f",userLocation.heading.x,userLocation.heading.y,userLocation.heading.z);
    
    MAAnnotationView *hearderAnnotationView = [self.mapView viewWithTag:kUserHearderAnnotaionViewTag];
    if (hearderAnnotationView)
    {
        hearderAnnotationView.transform = CGAffineTransformIdentity;
        CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI*userLocation.heading.magneticHeading/180.0);
        hearderAnnotationView.transform = transform;
    }
}

全部代码如下

#import "ViewController.h"
#import 
#import 

static const NSUInteger kUserHearderAnnotaionViewTag = 10000;

@interface ViewController ()

@property (nonatomic, strong) MAMapView *mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    [self creatMapView];
}

- (void)creatMapView {
    _mapView = [[MAMapView alloc]initWithFrame:self.view.bounds];
    _mapView.delegate = self;
    _mapView.showsCompass = NO;
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    //是否自定义用户位置精度圈
    _mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
    [self.view addSubview:_mapView];
}

#pragma mark MAMapViewDelegate
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    NSLog(@"%f,%f,%f",userLocation.heading.x,userLocation.heading.y,userLocation.heading.z);
    
    //通过hearderAnnotationView的tag值拿到当前位置的annotationView
    MAAnnotationView *hearderAnnotationView = [self.mapView viewWithTag:kUserHearderAnnotaionViewTag];
    if (hearderAnnotationView)
    {
        hearderAnnotationView.transform = CGAffineTransformIdentity;
        CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI*userLocation.heading.magneticHeading/180.0);
        hearderAnnotationView.transform = transform;
    }
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation {
    //用户当前位置大头针
    if ([annotation isKindOfClass:[MAUserLocation class]])
    {
        static NSString *kUserLocationStyleReuseIndetifier = @"userLocationStyleReuseIndetifier";
        
        MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:kUserLocationStyleReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kUserLocationStyleReuseIndetifier];
        }
        
        annotationView.canShowCallout = NO;
        annotationView.image = [UIImage imageNamed:@"heardImg_passenger_default"];
        annotationView.frame = CGRectMake(0, 0, 26, 26);
        annotationView.contentMode = UIViewContentModeScaleToFill;
        annotationView.layer.masksToBounds = YES;
        //设置当前位置大头针annotationView的tag值
        annotationView.tag = kUserHearderAnnotaionViewTag;
        
        return annotationView;
    }
    //其他大头针
    else if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        
        
    }
    return nil;
}
@end

最后

还有其他的实现方法,大家可以互相分享,希望大家提出宝贵意见。
gitHub地址https://github.com/Mexiang/GaoDeMapHeading

你可能感兴趣的:(高德地图当前位置图标旋转功能实现)