一、前奏
这里只是个人集成过程中的遇到的点,现做下标记。
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;
三、备注
详情还要看官方文档:地址
网友评论