集成iOS高德地图

一、前奏

这里只是个人集成过程中的遇到的点,现做下标记。

1,如果你需要集成涉及国外版地图,基于HTTPS的最新版本需要设置,需要注意基于国外定位,高德的不太精确(个人测试)

    [self.mapView performSelector:@selector(setShowsWorldMap:) withObject:@(YES)];

这个方法后台提供的国外地图是第三方的,是不稳定的,不推荐使用。集成国外版地图详情见:http://lbs.amap.com/dev/demo/switch-map#iOS

2,坐标转换

//将GPS转成高德坐标

CLLocationCoordinate2D amapcoord = MACoordinateConvert(CLLocationCoordinate2DMake(39.989612,116.480972),MACoordinateTypeGPS);

二、实战

1,创建地图

#import <MAMapKit/MAMapKit.h>

@property (nonatomic, strong)MAMapView *mapView; // 添加属性

// 添加到视图上
_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];

_mapView.delegate = self;

[self.view addSubview:_mapView];

2,设置中心点

CLLocationCoordinate2D centerPoint = CLLocationCoordinate2DMake(30.2740850000,120.1550700000);//纬度,经度

self.mapView.centerCoordinate = centerPoint; // 或者[self.mapView setCenterCoordinate:(centerPoint) animated:YES];

3,隐藏指南针

_mapView.showsCompass = NO;

4,隐藏比例尺

_mapView.showsScale = NO;

5,手势控制

_mapView.zoomEnabled = YES;    // NO表示禁用缩放手势,YES表示开启

_mapView.scrollEnabled = YES;  // NO表示禁用滑动手势,YES表示开启

6,设置地图语言(默认中文)

_mapView.language = MAMapLanguageEn;

7,添加标注(大头针)

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

pointAnnotation1.coordinate = CLLocationCoordinate2DMake(39.907465,116.337447);

pointAnnotation1.title = @"木樨地";

pointAnnotation1.subtitle = @"北京市西城区复兴门外大街29号楼三里河路";

[_mapView addAnnotation:pointAnnotation]; // 单个添加,多个添加[_mapView addAnnotations:@[pointAnnotation,pointAnnotation1,pointAnnotation2]];

8,自定义标注(大头针)

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation

{

if ([annotation isKindOfClass:[MAPointAnnotation class]])

{

// 自定义标注图标

static NSString *reuseIndetifier = @"annotationReuseIndetifier";

MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];

if (annotationView == nil)

{

annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation

reuseIdentifier:reuseIndetifier];

}

annotationView.image = [UIImage imageNamed:@"Point"]; // 换成你需要的标注图片

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

annotationView.centerOffset = CGPointMake(0, -9);

annotationView.canShowCallout = YES;    // 设置气泡可以弹出,默认为NO

annotationView.draggable = NO;        // 设置标注可以拖动,默认为NO

return annotationView;

/* 默认的标注图标

static NSString *pointReuseIndentifier = @"pointReuseIndentifier";

MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];

if (annotationView == nil)

{

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

}

annotationView.image = [UIImage imageNamed:@"Point"];

annotationView.canShowCallout = YES;  // 设置气泡可以弹出,默认为NO

annotationView.animatesDrop = YES;    // 设置标注动画显示,默认为NO

annotationView.draggable = YES;        // 设置标注可以拖动,默认为NO

annotationView.pinColor = MAPinAnnotationColorPurple;

return annotationView;

*/

}

return nil;

}

9,绘制折线

- (void)drawLine{

//构造折线数据对象

CLLocationCoordinate2D commonPolylineCoords[3];

commonPolylineCoords[0].latitude  = 39.957312;

commonPolylineCoords[0].longitude = 116.416261;

commonPolylineCoords[1].latitude  = 39.907465;

commonPolylineCoords[1].longitude = 116.337447;

commonPolylineCoords[2].latitude  = 39.991720;

commonPolylineCoords[2].longitude = 116.271057;

//构造折线对象

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

//在地图上添加折线对象

[_mapView addOverlay: commonPolyline];

}

// 设置折线的样式- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id)overlay

{

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

{

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

polylineView.lineWidth = 5.f; // 折线的宽度

polylineView.strokeColor = [UIColor redColor]; // 折线的颜色

// 端点类型

polylineView.lineCap = kCGLineCapSquare;

// 连接类型

polylineView.lineJoin = kCGLineJoinBevel;

return polylineView;

}

return nil;

}

9.给标注点添加不同的图标

// 根据anntation生成对应的View- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation

{

if ([annotation isKindOfClass:[MAPointAnnotation class]])

{

// 自定义标注图标

static NSString *reuseIndetifier = @"annotationReuseIndetifier";

MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];

if (annotationView == nil)

{

annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation

reuseIdentifier:reuseIndetifier];

}

annotationView.image = [UIImage imageNamed:@"Point"];

if ([annotation.title isEqualToString:@"海神庙"]) {

annotationView.image = [UIImage imageNamed:@"Point"];

}

if ([annotation.title isEqualToString:@"金巴兰海滩"]) {

annotationView.image = [UIImage imageNamed:@"pointNew"];

}

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

annotationView.centerOffset = CGPointMake(0, -9);

annotationView.canShowCallout = YES;    // 设置气泡可以弹出,默认为NO

annotationView.draggable = NO;        // 设置标注可以拖动,默认为NO

return annotationView;

/* 默认的标注图标

static NSString *pointReuseIndentifier = @"pointReuseIndentifier";

MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];

if (annotationView == nil)

{

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

}

annotationView.image = [UIImage imageNamed:@"Point"];

annotationView.canShowCallout = YES;  // 设置气泡可以弹出,默认为NO

annotationView.animatesDrop = YES;    // 设置标注动画显示,默认为NO

annotationView.draggable = YES;        // 设置标注可以拖动,默认为NO

annotationView.pinColor = MAPinAnnotationColorPurple;

return annotationView;

*/

}

return nil;

}

10.点击标注点,触发不同的事件

通过协议方法进行判断

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view

根据点击不同的标注点数据进行处理

MAPointAnnotation *pointAnnotation = (MAPointAnnotation *)mapView.selectedAnnotations[0];

NSLog(@"city =>%@,subtitle =>%@",pointAnnotation.title,pointAnnotation.subtitle);

例如:

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view{

NSLog(@"selectedAnnotations ==》%@",mapView.selectedAnnotations);

if (mapView.selectedAnnotations.count == 0) {

return;

}

MAPointAnnotation *pointAnnotation = (MAPointAnnotation *)mapView.selectedAnnotations[0];

NSLog(@"city =>%@,subtitle =>%@",pointAnnotation.title,pointAnnotation.subtitle);

for (int i = 0; i < self.addressArray.count; i++) {

AddressModel *model = (AddressModel *)self.addressArray[i];

if ([pointAnnotation.title isEqualToString:model.cityName]) {

// 给底部view赋值

if (self.bottomView.hidden) {

self.bottomView.hidden = NO;

}

self.bottomView.cityNameLable.text = model.cityName;

self.bottomView.cityEnglishLable.text = model.detail;

self.bottomView.avgpriceLable.text = [NSString stringWithFormat:@"人均:%d",i*100];

break;

}

}

}

11.在地图上显示所有标注点,通过协议

- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated;

在视线内显示所有的标注,会自动计算最佳缩放比以将所有点显示出来

12.默认显示气泡

    [_mapView selectAnnotation:pointAnnotation animated:YES];

13.自定义标注点,例如更换图标

在 协议的回调函数mapView:viewForAnnotation:中修改 MAAnnotationView 对应的标注图片。示例代码如下:

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        MAPinAnnotationView  *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];

        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }

        // 设置自定义图标
        annotationView.image = [UIImage imageNamed:@"price_lv5_yellow"];
       
        // 设置中心点偏移,使得标注底部中间点成为经纬度对应点
        annotationView.centerOffset = CGPointMake(0, -18);
       
//        如果要自定义标注点图标,一定要注释掉下面几行
//        annotationView.animatesDrop = YES;        //设置标注动画显示,默认为NO
//        annotationView.draggable = YES;           //设置标注可以拖动,默认为NO
//        annotationView.pinColor = MAPinAnnotationColorPurple;

        return annotationView;
    }

    return nil;
}

一定要注释掉下面几行,否则自定义的图标不会显示

//        annotationView.animatesDrop = YES;        //设置标注动画显示,默认为NO

//        annotationView.draggable = YES;           //设置标注可以拖动,默认为NO

//        annotationView.pinColor = MAPinAnnotationColorPurple;

三、备注

详情还要看官方文档:地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,980评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,422评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,130评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,553评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,408评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,326评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,720评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,373评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,678评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,722评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,486评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,335评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,738评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,283评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,692评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,893评论 2 335

推荐阅读更多精彩内容