實(shí)現(xiàn)思路
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、實(shí)現(xiàn)地圖添加大頭針的代理,設(shè)置當(dāng)前位置圖標(biāo)
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
//用戶當(dāng)前位置大頭針
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蚜锨、在地圖時(shí)時(shí)位置更新的方法里實(shí)現(xiàn)旋轉(zhuǎn)功能
- (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值拿到當(dāng)前位置的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 {
//用戶當(dāng)前位置大頭針
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;
//設(shè)置當(dāng)前位置大頭針annotationView的tag值
annotationView.tag = kUserHearderAnnotaionViewTag;
return annotationView;
}
//其他大頭針
else if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
}
return nil;
}
@end
最后
還有其他的實(shí)現(xiàn)方法,大家可以互相分享慢蜓,希望大家提出寶貴意見亚再。
gitHub地址https://github.com/Mexiang/GaoDeMapHeading