高德地图初步使用-显示

#import "ViewController.h"

#import <MAMapKit/MAMapKit.h>

#import "CustomAnnotationView.h"

#import "CustomCalloutView.h"

@interface ViewController ()<MAMapViewDelegate>


@property(nonatomic, strong)MAMapView *mapView;


@end


@implementation ViewController


-(void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    

    

    /**

     *cocoapods导入高德地图包

     *更改plist配置

     *为了能正常调起高德地图App的功能plist里增加:

     

                    <key>LSApplicationQueriesSchemes</key>

                    <array>

                    <string>iosamap</string>

                    </array>

     字段.

     */

    

    

#warning 1.首先声明MAPointAnnotation 来确定大头针位置及点击大头针弹出气泡的内容(默认只有titlesubtitle)

    /**

     每一个大头针里面都自带一个calloutView 点击就显示出来,我们设置MApointAnnotation实质上是在设置callout内容和大头针的位置,不过自定义了气泡的话则需要从新给气泡的其他属性赋值,customAnnotationview里有写到

     

     */

    

    //间接制作弹出气泡(相当于 cell赋值的Model) ,如果自定义气泡则需要单独给定义的气泡写其他赋值的属性(除这两个之外的)

    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc]init];

    pointAnnotation.coordinate = CLLocationCoordinate2DMake(39.989631, 116.481018);

    pointAnnotation.title = @"A";

    pointAnnotation.subtitle = @"假的";

    

    MAPointAnnotation *pointAnnotation1 = [[MAPointAnnotation alloc]init];

    pointAnnotation1.coordinate = CLLocationCoordinate2DMake(40.389631, 116.481018);

    pointAnnotation1.title = @"B";

    pointAnnotation1.subtitle = @"真的";

    

    MAPointAnnotation *pointAnnotation2 = [[MAPointAnnotation alloc]init];

    pointAnnotation2.coordinate = CLLocationCoordinate2DMake(36.989631, 116.481018);

    pointAnnotation2.title = @"C";

    pointAnnotation2.subtitle = @"哈哈";

    

    MAPointAnnotation *pointAnnotation3 = [[MAPointAnnotation alloc]init];

    pointAnnotation3.coordinate = CLLocationCoordinate2DMake(39.989631, 116.081018);

    pointAnnotation3.title = @"D";

    pointAnnotation3.subtitle = @"吼吼";

    

    //添加给mapView

    [_mapView addAnnotations:@[pointAnnotation, pointAnnotation1,pointAnnotation2,pointAnnotation3]];


    

    

}


- (void)viewDidLoad {

    [super viewDidLoad];

    

#warning 2.初始化 签代理 添加到View

    //初始化一个地图View(可以理解成是一个tableView)

    _mapView = [[MAMapView alloc]initWithFrame:self.view.frame];

    //代理千万别忘了签

    _mapView.delegate = self;

    

//    _mapView.language = MAMapLanguageEn;//可以设置语言!

    

    //添加到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];

    commonPolylineCoords[0].latitude = 39.832136;

    commonPolylineCoords[0].longitude = 116.34095;

    

    commonPolylineCoords[1].latitude = 39.832136;

    commonPolylineCoords[1].longitude = 116.42095;

    

    commonPolylineCoords[2].latitude = 39.902136;

    commonPolylineCoords[2].longitude = 116.42095;

    

    commonPolylineCoords[3].latitude = 39.902136;

    commonPolylineCoords[3].longitude = 116.44095;

    

    //构造折线对象

    MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:4];

    

    //在地图上添加折线对象,然后去实现代理方法2

    [_mapView addOverlay: commonPolyline];

    

    //还可构造多边形 大地曲线 图片覆盖物

    

    //还可设置自定义图层 这些都在开发指南上有详细的例子

}


#warning 3.添加大头针

#pragma mark - 添加视图内的大头针

-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation

{//相当于tableViewcellForRow方法,可以实现重用的

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

//        }

        

#warning 4.自定义气泡 需要自定义大头针继承MAAnnotationViewMAPinAnnotationView。若继承MAAnnotationView,则需要设置标注图标;若继承MAPinAnnotationView,使用默认的大头针标注。

        

        /**

        因为继承了AnnotationView所以会有annotation这个属性,1 步设置会把值传给annotation(推测),所以把我们自定义的titlesubtitle等于annotation.titlesubtitle就行了,自定义的其他属性单独赋值就好

        */

//        自定义制作一个大头针,相当于cell<还可以自定义,具体实现参考开发指南>

        CustomAnnotationView *annotationView = (CustomAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];

        if (annotationView == nil) {

            annotationView = [[CustomAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];

        }

        

        //默认 设置大头针属性

//        annotationView.canShowCallout = YES;//能弹出气泡

//        annotationView.animatesDrop = YES;//动画效果

//        annotationView.draggable = YES;//可移动

//        annotationView.pinColor = MAPinAnnotationColorPurple;

        

        //默认 把大头针变图片

//        annotationView.image = [UIImage imageNamed:@"IMG_7476.jpg"];//设置图片的

        //设置中心点偏移,使得标注底部中间点成为经纬度对应点

//        annotationView.centerOffset = CGPointMake(0, -18);

        

        //自定义的大头针

        annotationView.draggable = YES;

        annotationView.canShowCallout = NO;//如果自定义了callout就要关掉自带的

        annotationView.image1 = [UIImage imageNamed:@"IMG_7476.jpg"];

        return annotationView;

    }

    return nil;

}


#pragma mark - 协议中的mapView:viewForOverlay:回调函数,设置折线的样式

-(MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id<MAOverlay>)overlay

{

    if ([overlay isKindOfClass:[MAPolyline class]])

    {

        MAPolylineView *polylineView = [[MAPolylineView alloc] initWithPolyline:overlay];

        

        polylineView.lineWidth = 10.f;

        polylineView.strokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.6];

        

        return polylineView;

    }

    return nil;

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值