2、
自定義 線的圖片,只需要在 rendererForOverlay 方法中,設置:
polylineRenderer.strokeImage = [UIImage imageNamed:@"jiantouD"]; //即可顯示
注意圖片的格式:用於生成筆觸紋理id的圖片(image需滿足: 長寬相等,且寬度值為2的整數次冪; 如果您需要減輕繪制產生的鋸齒,您可以參考AMap.bundle中的traffic_texture_blue.png的方式,在image兩邊增加部分透明像素.)。(簡單說就是正方的圖片,又明確方向的圖片,默認方向為向下)
1、
demo : https://github.com/xushiyou23/AMapTesting
利用高德繪制線:1、繪制線 坐標數組每0.02s增加一次坐標點--2、移除原來點繪制線--3、添加新的+每繪制3次,讓繪制線最后3組坐標在屏幕中 居中----循環1-3;最后讓繪制線居中在屏幕中即可
代碼:
#import "HomeViewController.h" ///繪制間隔 #define huizhiTimes 0.02
///居中點的個數
#define IntheMiddlePoint 2
///每次畫線跳躍幾個點
#define jumpPoints 3
@interface HomeViewController ()<MAMapViewDelegate>{ //繪制了多少點 和總個數對比 NSInteger huizhiNum; //繪制線 MAPolyline *commonPolyline; //結束繪制 BOOL endHuizhi; } ///需要居中顯示的點 @property(nonatomic,strong) NSMutableArray * TenPointArray ; @property (nonatomic, strong) NSMutableArray * pointArray; @end @implementation HomeViewController ///觸摸移動 開始繪制 -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self mapViewHUIZHI]; } - (void)viewDidLoad { [super viewDidLoad]; // 加載地圖 [self setMap]; [self.mapViewhome setMapType:MAMapTypeStandard]; self.pointArray=[NSMutableArray array]; } #pragma mark Map -(void)setMap{ ///初始化地圖 self.mapViewhome = [[MAMapView alloc] initWithFrame:self.view.frame]; self.mapViewhome.showsCompass= NO; // 設置成NO表示關閉指南針;YES表示顯示指南針 ///如果您需要進入地圖就顯示定位小藍點,則需要下面兩行代碼 self.mapViewhome.showsUserLocation = NO; [self.mapViewhome setZoomLevel:18 animated:YES]; self.mapViewhome.userTrackingMode = MAUserTrackingModeFollow; self.mapViewhome.delegate =self; ///地圖需要v4.5.0及以上版本才必須要打開此選項(v4.5.0以下版本,需要手動配置info.plist) [AMapServices sharedServices].enableHTTPS = YES; self.mapViewhome.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; ///把地圖添加至view self.mapViewhome.mapType = MAMapTypeNavi; [self.view addSubview:self.mapViewhome]; } #pragma mark ---------------------------------繪制軌跡- //創建數據 -(void)huizhiData{ huizhiNum = 0; _TenPointArray = [NSMutableArray array]; //di 中存放坐標 NSDictionary * di = @{@"latitude":dict[@"latitude"],@"longitude":dict[@"longitude"]}; //坐標數組 [self.pointArray addObject:di]; } //繪制線 - (void)mapViewHUIZHI{ huizhiNum +=jumpPoints ; if (huizhiNum>(_pointArray.count-4)) { huizhiNum =_pointArray.count-1; endHuizhi = YES; } CLLocationCoordinate2D commonPolylineCoords[huizhiNum]; for (int i=0; i<huizhiNum; i++) { NSDictionary * dic = self.pointArray[i]; commonPolylineCoords[i].latitude= [dic[@"latitude"] doubleValue]; commonPolylineCoords[i].longitude=[dic[@"longitude"] doubleValue]; } [self.mapViewhome removeOverlay:commonPolyline]; //構造折線對象 commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:huizhiNum]; //在地圖上添加折線對象 [self.mapViewhome addOverlay: commonPolyline]; //設置地圖中心位置 NSDictionary * huizhiDic2 = self.pointArray[huizhiNum]; MAPointAnnotation * a1= [[MAPointAnnotation alloc] init]; a1.coordinate = CLLocationCoordinate2DMake([huizhiDic2[@"latitude"] doubleValue], [ huizhiDic2[@"longitude"] doubleValue]); //划線 顯示進行中的后10個點 if (_TenPointArray.count<IntheMiddlePoint)
{ [_TenPointArray addObject:a1];
}else{
[_TenPointArray replaceObjectAtIndex:0 withObject:a1];
} //設置地圖中心位置
if(endHuizhi){
[self.mapViewhome showOverlays:@[commonPolyline] edgePadding:UIEdgeInsetsMake(IPHONEHIGHT(400), IPHONEWIDTH(200), IPHONEHIGHT(400), IPHONEWIDTH(200)) animated:YES];
huizhiNum = 0;
}else{ if (huizhiNum%9==0)
{
//開始居中 后面的點
[self.mapViewhome showAnnotations:_TenPointArray edgePadding:UIEdgeInsetsMake(IPHONEHIGHT(500), IPHONEWIDTH(300), IPHONEHIGHT(400), IPHONEWIDTH(200)) animated:YES];
} } }
#pragma mark - MAMapViewDelegate 樣式
-(MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay {
//繪制線
if ([overlay isKindOfClass:[MAPolyline class]]) {
if(huizhiNum<1500){
[self performSelector:@selector(mapViewHUIZHI) withObject:nil afterDelay:huizhiTimes];
}
MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
polylineRenderer.lineWidth= 3.f;
polylineRenderer.strokeColor= [UIColor greenColor];
polylineRenderer.lineJoinType = kMALineJoinRound;
polylineRenderer.lineCapType= kMALineCapRound;
return polylineRenderer;
}
return nil;
}
@end