一、前奏
這里只是個(gè)人集成過(guò)程中的遇到的點(diǎn),現(xiàn)做下標(biāo)記泼差。
1,如果你需要集成涉及國(guó)外版地圖,基于HTTPS的最新版本需要設(shè)置,需要注意基于國(guó)外定位呵俏,高德的不太精確(個(gè)人測(cè)試)
? ? [self.mapView performSelector:@selector(setShowsWorldMap:) withObject:@(YES)];
這個(gè)方法后臺(tái)提供的國(guó)外地圖是第三方的堆缘,是不穩(wěn)定的,不推薦使用普碎。集成國(guó)外版地圖詳情見(jiàn):http://lbs.amap.com/dev/demo/switch-map#iOS
2吼肥,坐標(biāo)轉(zhuǎn)換
//將GPS轉(zhuǎn)成高德坐標(biāo)
CLLocationCoordinate2D amapcoord = MACoordinateConvert(CLLocationCoordinate2DMake(39.989612,116.480972),MACoordinateTypeGPS);
二、實(shí)戰(zhàn)
1麻车,創(chuàng)建地圖
#import <MAMapKit/MAMapKit.h>
@property (nonatomic, strong)MAMapView *mapView; // 添加屬性
// 添加到視圖上
_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
_mapView.delegate = self;
[self.view addSubview:_mapView];
2缀皱,設(shè)置中心點(diǎn)
CLLocationCoordinate2D centerPoint = CLLocationCoordinate2DMake(30.2740850000,120.1550700000);//緯度,經(jīng)度
self.mapView.centerCoordinate = centerPoint; // 或者[self.mapView setCenterCoordinate:(centerPoint) animated:YES];
3绪氛,隱藏指南針
_mapView.showsCompass = NO;
4唆鸡,隱藏比例尺
_mapView.showsScale = NO;
5,手勢(shì)控制
_mapView.zoomEnabled = YES;? ? // NO表示禁用縮放手勢(shì)枣察,YES表示開(kāi)啟
_mapView.scrollEnabled = YES;? // NO表示禁用滑動(dòng)手勢(shì)争占,YES表示開(kāi)啟
6,設(shè)置地圖語(yǔ)言(默認(rèn)中文)
_mapView.language = MAMapLanguageEn;
7序目,添加標(biāo)注(大頭針)
MAPointAnnotation *pointAnnotation1 = [[MAPointAnnotation alloc] init];
pointAnnotation1.coordinate = CLLocationCoordinate2DMake(39.907465,116.337447);
pointAnnotation1.title = @"木樨地";
pointAnnotation1.subtitle = @"北京市西城區(qū)復(fù)興門(mén)外大街29號(hào)樓三里河路";
[_mapView addAnnotation:pointAnnotation]; // 單個(gè)添加臂痕,多個(gè)添加[_mapView addAnnotations:@[pointAnnotation,pointAnnotation1,pointAnnotation2]];
8,自定義標(biāo)注(大頭針)
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
// 自定義標(biāo)注圖標(biāo)
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"]; // 換成你需要的標(biāo)注圖片
// 設(shè)置中心點(diǎn)偏移猿涨,使得標(biāo)注底部中間點(diǎn)成為經(jīng)緯度對(duì)應(yīng)點(diǎn)
annotationView.centerOffset = CGPointMake(0, -9);
annotationView.canShowCallout = YES;? ? // 設(shè)置氣泡可以彈出握童,默認(rèn)為NO
annotationView.draggable = NO;? ? ? ? // 設(shè)置標(biāo)注可以拖動(dòng),默認(rèn)為NO
return annotationView;
/* 默認(rèn)的標(biāo)注圖標(biāo)
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;? // 設(shè)置氣泡可以彈出叛赚,默認(rèn)為NO
annotationView.animatesDrop = YES;? ? // 設(shè)置標(biāo)注動(dòng)畫(huà)顯示澡绩,默認(rèn)為NO
annotationView.draggable = YES;? ? ? ? // 設(shè)置標(biāo)注可以拖動(dòng),默認(rèn)為NO
annotationView.pinColor = MAPinAnnotationColorPurple;
return annotationView;
*/
}
return nil;
}
9俺附,繪制折線
- (void)drawLine{
//構(gòu)造折線數(shù)據(jù)對(duì)象
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;
//構(gòu)造折線對(duì)象
MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:3];
//在地圖上添加折線對(duì)象
[_mapView addOverlay: commonPolyline];
}
// 設(shè)置折線的樣式- (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]; // 折線的顏色
// 端點(diǎn)類(lèi)型
polylineView.lineCap = kCGLineCapSquare;
// 連接類(lèi)型
polylineView.lineJoin = kCGLineJoinBevel;
return polylineView;
}
return nil;
}
9.給標(biāo)注點(diǎn)添加不同的圖標(biāo)
// 根據(jù)anntation生成對(duì)應(yīng)的View- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
// 自定義標(biāo)注圖標(biāo)
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"];
}
// 設(shè)置中心點(diǎn)偏移肥卡,使得標(biāo)注底部中間點(diǎn)成為經(jīng)緯度對(duì)應(yīng)點(diǎn)
annotationView.centerOffset = CGPointMake(0, -9);
annotationView.canShowCallout = YES;? ? // 設(shè)置氣泡可以彈出,默認(rèn)為NO
annotationView.draggable = NO;? ? ? ? // 設(shè)置標(biāo)注可以拖動(dòng)事镣,默認(rèn)為NO
return annotationView;
/* 默認(rèn)的標(biāo)注圖標(biāo)
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;? // 設(shè)置氣泡可以彈出步鉴,默認(rèn)為NO
annotationView.animatesDrop = YES;? ? // 設(shè)置標(biāo)注動(dòng)畫(huà)顯示,默認(rèn)為NO
annotationView.draggable = YES;? ? ? ? // 設(shè)置標(biāo)注可以拖動(dòng),默認(rèn)為NO
annotationView.pinColor = MAPinAnnotationColorPurple;
return annotationView;
*/
}
return nil;
}
10.點(diǎn)擊標(biāo)注點(diǎn)氛琢,觸發(fā)不同的事件
通過(guò)協(xié)議方法進(jìn)行判斷
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
根據(jù)點(diǎn)擊不同的標(biāo)注點(diǎn)數(shù)據(jù)進(jìn)行處理
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.在地圖上顯示所有標(biāo)注點(diǎn)喊递,通過(guò)協(xié)議
- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated;
在視線內(nèi)顯示所有的標(biāo)注,會(huì)自動(dòng)計(jì)算最佳縮放比以將所有點(diǎn)顯示出來(lái)
12.默認(rèn)顯示氣泡
??? [_mapView selectAnnotation:pointAnnotation animated:YES];
13.自定義標(biāo)注點(diǎn),例如更換圖標(biāo)
在 協(xié)議的回調(diào)函數(shù)mapView:viewForAnnotation:中修改 MAAnnotationView 對(duì)應(yīng)的標(biāo)注圖片阳似。示例代碼如下:
- (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];
??????? }
??????? // 設(shè)置自定義圖標(biāo)
??????? annotationView.image = [UIImage imageNamed:@"price_lv5_yellow"];
???????
??????? // 設(shè)置中心點(diǎn)偏移骚勘,使得標(biāo)注底部中間點(diǎn)成為經(jīng)緯度對(duì)應(yīng)點(diǎn)
??????? annotationView.centerOffset = CGPointMake(0, -18);
???????
//??????? 如果要自定義標(biāo)注點(diǎn)圖標(biāo),一定要注釋掉下面幾行
//??????? annotationView.animatesDrop = YES;??????? //設(shè)置標(biāo)注動(dòng)畫(huà)顯示障般,默認(rèn)為NO
//??????? annotationView.draggable = YES;?????????? //設(shè)置標(biāo)注可以拖動(dòng)调鲸,默認(rèn)為NO
//??????? annotationView.pinColor = MAPinAnnotationColorPurple;
??????? return annotationView;
??? }
??? return nil;
}
一定要注釋掉下面幾行,否則自定義的圖標(biāo)不會(huì)顯示
//??????? annotationView.animatesDrop = YES;??????? //設(shè)置標(biāo)注動(dòng)畫(huà)顯示挽荡,默認(rèn)為NO
//??????? annotationView.draggable = YES;?????????? //設(shè)置標(biāo)注可以拖動(dòng)藐石,默認(rèn)為NO
//??????? annotationView.pinColor = MAPinAnnotationColorPurple;
三、備注
詳情還要看官方文檔:地址