iOS 高德自定义坐标轨迹绘制动画 类似与Keep的轨迹绘制

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

 

posted @ 2018-09-06 19:42  徐家汇  阅读(6603)  评论(0编辑  收藏  举报