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

实现思路

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<MAAnnotation>)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 <AMapFoundationKit/AMapFoundationKit.h>
#import <MAMapKit/MAMapKit.h>

static const NSUInteger kUserHearderAnnotaionViewTag = 10000;

@interface ViewController ()<MAMapViewDelegate,CLLocationManagerDelegate>

@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<MAAnnotation>)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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,249评论 25 708
  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 现在很多社交、...
    JJO阅读 4,371评论 4 19
  • http://www.cnblogs.com/kenshincui/p/4125570.html 摘要: 现在很多...
    大崔老师阅读 3,535评论 1 2
  • 情商这个东西到底是什么?说不准,会不会为人处事?为人处事有什么标准?怎么评判? 就比如今天一块儿去面试,等通知,晚...
    雾是成风阅读 226评论 0 0
  • 今天学习了会议设计,原来会议也可以事先进行设计的。其实,我们每个人都参加过大大小小很多次会议,有的人也主持或主导过...
    白云路阅读 492评论 0 0

友情链接更多精彩内容