#import "ViewController.h"
#import <MAMapKit/MAMapKit.h>
#import "CustomAnnotationView.h"
#import "CustomCalloutView.h"
@interface  ViewController ()<MAMapViewDelegate>
 
(nonatomic, strong)MAMapView *mapView;
 
 @end
 
@implementation
 
void)viewDidAppear:(BOOL)animated
 {
super viewDidAppear:animated];
     
     
/**
导入高德地图包
更改plist配置
     *为了能正常调起高德地图App的功能plist里增加:
      
                     <key>LSApplicationQueriesSchemes</key>
                     <array>
                     <string>iosamap</string>
                     </array>
字段.
      */
     
     
首先声明MAPointAnnotation 来确定大头针位置及点击大头针弹出气泡的内容(默认只有title和subtitle)
/**
     每一个大头针里面都自带一个calloutView 点击就显示出来,我们设置MApointAnnotation实质上是在设置callout内容和大头针的位置,不过自定义了气泡的话则需要从新给气泡的其他属性赋值,在customAnnotationview里有写到
      
      */
     
     //间接制作弹出气泡(相当于 给cell赋值的Model) ,如果自定义气泡则需要单独给定义的气泡写其他赋值的属性(除这两个之外的)
    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc]init];
coordinate =  CLLocationCoordinate2DMake(39.989631,  116.481018);
title =  @"小A";
subtitle =  @"假的";
     
    MAPointAnnotation *pointAnnotation1 = [[MAPointAnnotation alloc]init];
coordinate =  CLLocationCoordinate2DMake(40.389631,  116.481018);
title =  @"小B";
subtitle =  @"真的";
     
    MAPointAnnotation *pointAnnotation2 = [[MAPointAnnotation alloc]init];
coordinate =  CLLocationCoordinate2DMake(36.989631,  116.481018);
title =  @"小C";
subtitle =  @"哈哈";
     
    MAPointAnnotation *pointAnnotation3 = [[MAPointAnnotation alloc]init];
coordinate =  CLLocationCoordinate2DMake(39.989631,  116.081018);
title =  @"小D";
subtitle =  @"吼吼";
     
    //添加给mapView
_mapView addAnnotations:@[pointAnnotation, pointAnnotation1,pointAnnotation2,pointAnnotation3]];
 
     
     
 }
 
void)viewDidLoad {
    [super viewDidLoad];
     
初始化 签代理  添加到View
    //初始化一个地图View(可以理解成是一个tableView)
    _mapView = [[MAMapView alloc]initWithFrame:self.view.frame];
     //代理千万别忘了签
    _mapView.delegate = self;
     
可以设置语言!
     
    //添加到View上
self.view addSubview:_mapView];
     
    //地图logo 默认开启
    _mapView.logoCenter = _mapView.logoCenter = CGPointMake(CGRectGetWidth(self.view.bounds)-55, 450);
    //指南针 默认开启
    _mapView.showsCompass= YES;  // 设置成NO表示关闭指南针;YES表示显示指南针
     
    _mapView.compassOrigin= CGPointMake(_mapView.compassOrigin.x, 22);  //设置指南针位置
     
    //比例尺 默认开启
    _mapView.showsScale = YES;//设置成NO表示不显示比例尺;YES表示显示比例尺
     
    _mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x, 22);   //设置比例尺位置
     
    //可以添加手势 略
     
/**
     iOS SDK支持对选定的屏幕地图区域(CGRect)进行截屏,截取的内容包括:地图、地图覆盖物、弹出气泡。
      
     说明:地图截屏功能依赖于地图显示,即:只有内容先显示在地图上,才能进行截屏。
      
使用 MAMapView 中的 takeSnapshotInRect 方法进行截屏,该方法返回UIImage对象
      */
CGRect inRect =  CGRectMake(80,142,160,284);
UIImage *screenshotImage = [_mapView takeSnapshotInRect:inRect];
     
    //实时交通图
 //    _mapView.showTraffic = YES;
     
     //构造折线数据对象
CLLocationCoordinate2D commonPolylineCoords[4];
0].latitude = 39.832136;
0].longitude = 116.34095;
     
1].latitude = 39.832136;
1].longitude = 116.42095;
     
2].latitude = 39.902136;
2].longitude = 116.42095;
     
3].latitude = 39.902136;
3].longitude = 116.44095;
     
    //构造折线对象
MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords  count:4];
     
     //在地图上添加折线对象,然后去实现代理方法2
_mapView addOverlay: commonPolyline];
     
     //还可构造多边形 圆 大地曲线 图片覆盖物
     
     //还可设置自定义图层 这些都在开发指南上有详细的例子
 }
 
添加大头针
添加视图内的大头针
MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{//相当于tableView的cellForRow方法,可以实现重用的
if ([annotation  isKindOfClass:[MAPointAnnotation  class]]) {
static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
         
         //默认 制作一个大头针,相当于cell<还可以自定义,具体实现参考开发指南>
 //        MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
 //        if (annotationView == nil) {
 //            annotationView = [[MAPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
 //        }
         
自定义气泡 需要自定义大头针继承MAAnnotationView或MAPinAnnotationView。若继承MAAnnotationView,则需要设置标注图标;若继承MAPinAnnotationView,使用默认的大头针标注。
         
/**
因为继承了AnnotationView所以会有annotation这个属性,第1 步设置会把值传给annotation(推测),所以把我们自定义的title和subtitle等于annotation.title和subtitle就行了,自定义的其他属性单独赋值就好
         */
//        自定义制作一个大头针,相当于cell<还可以自定义,具体实现参考开发指南>
CustomAnnotationView *annotationView = (CustomAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
if (annotationView ==  nil) {
CustomAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
         }
         
        //默认 设置大头针属性
能弹出气泡
动画效果
可移动
 //        annotationView.pinColor = MAPinAnnotationColorPurple;
         
        //默认 把大头针变图片
设置图片的
         //设置中心点偏移,使得标注底部中间点成为经纬度对应点
 //        annotationView.centerOffset = CGPointMake(0, -18);
         
        //自定义的大头针
draggable =  YES;
canShowCallout =  NO;//如果自定义了callout就要关掉自带的
image1 = [UIImage imageNamed:@"IMG_7476.jpg"];
return
     }
    return  nil;
 }
 
协议中的mapView:viewForOverlay:回调函数,设置折线的样式
MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id<MAOverlay>)overlay
 {
if ([overlay isKindOfClass:[MAPolyline class]])
     {
MAPolylineView *polylineView = [[MAPolylineView alloc] initWithPolyline:overlay];
         
lineWidth =  10.f;
strokeColor = [UIColor colorWithRed:0 green:0  blue:1 alpha:0.6];
         
return
     }
    return  nil;
 }