效果图:
Snip20170727_6.png
废话不多说,直接上代码
//画历史轨迹并进行轨迹纠偏
-(void)historyCorrect:(NSArray<MATraceLocation *> *)Arr{
MATraceManager *manager = [[MATraceManager alloc] init];
//[self clear];
__weak typeof(self) weakSelf = self;
__unused NSOperation *op = [manager queryProcessedTraceWith:Arr type:-1 processingCallback:^(int index, NSArray<MATracePoint *> *points) {
[weakSelf addSubTrace:points toMapView:weakSelf.mapV];
} finishCallback:^(NSArray<MATracePoint *> *points, double distance) {
NSLog(@"numPoint: %lu",(unsigned long)points.count);
sumDistance = [NSString stringWithFormat:@"总里程:%fkm",distance];
weakSelf.queryOperation = nil;
[weakSelf addFullTrace:points toMapView:weakSelf.mapV];
} failedCallback:^(int errorCode, NSString *errorDesc) {
NSLog(@"Error: %@", errorDesc);
weakSelf.queryOperation = nil;
}];
self.queryOperation = op;
}
- (void)addFullTrace:(NSArray<MATracePoint*> *)tracePoints toMapView:(MAMapView *)mapView{
//获取起始点和终点
[self optainStartPointEndPoint:tracePoints];
//此类用于定义一个由多个点相连的多段线
MAMultiPolyline *polyline = [self makePolyLineWith:tracePoints];
if(!polyline) {
return;
}
[mapView setVisibleMapRect:polyline.boundingMapRect edgePadding:UIEdgeInsetsMake(30, 70, 30, 50) animated:YES];
[self.processedOverlays addObject:polyline];
[mapView addOverlays:self.processedOverlays];
}
- (void)addSubTrace:(NSArray<MATracePoint*> *)tracePoints toMapView:(MAMapView *)mapView {
MAMultiPolyline *polyline = [self makePolyLineWith:tracePoints];
if(!polyline) {
return;
}
[mapView removeOverlay:polyline];
[self.processedOverlays removeAllObjects];
MAMapRect visibleRect = [mapView visibleMapRect];
if(!MAMapRectContainsRect(visibleRect, polyline.boundingMapRect)) {
MAMapRect newRect = MAMapRectUnion(visibleRect, polyline.boundingMapRect);
[mapView setVisibleMapRect:newRect];
}
[self.processedOverlays addObject:polyline];
[mapView addOverlay:polyline];
}
//划线
- (MAMultiPolyline *)makePolyLineWith:(NSArray<MATracePoint*> *)tracePoints {
if(tracePoints.count == 0) {
return nil;
}
CLLocationCoordinate2D *pCoords = malloc(sizeof(CLLocationCoordinate2D) * tracePoints.count);
if(!pCoords) {
return nil;
}
for(int i = 0; i < tracePoints.count; ++i) {
MATracePoint *p = [tracePoints objectAtIndex:i];
CLLocationCoordinate2D *pCur = pCoords + i;
pCur->latitude = p.latitude;
pCur->longitude = p.longitude;
}
//分段绘制,根据经纬度坐标数据生成多段线
MAMultiPolyline *polyline = [MAMultiPolyline polylineWithCoordinates:pCoords count:tracePoints.count drawStyleIndexes:@[@10, @60]];
if(pCoords) {
free(pCoords);
}
return polyline;
}
//绘制覆盖物
#pragma mark - MAMapViewDelegate
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
if ([overlay isKindOfClass:[MAMultiPolyline class]])
{
MAPolylineRenderer *polylineView = [[MAPolylineRenderer alloc] initWithPolyline:(MAPolyline *)overlay];
polylineView.lineWidth = 8.f;
//路线填充图片
[polylineView loadStrokeTextureImage:[UIImage imageNamed:@"arrowTexture.png"]];
return polylineView;
}
return nil;
}
到这里轨迹纠偏划线就好了.接下来开始添加平滑移动动画
self.annotation.coordinate = coords1[0];
MAAnimatedAnnotation *anno = self.annotation;
CLLocationCoordinate2D coords[1];
__weak __typeof(anno) WeakAnno = anno;
// 添加移动动画 ***************************************
//这里要注意.这个方法是在返回数据里面控制每一个点进行相应的赋值
[anno addMoveAnimationWithKeyCoordinates:coords count:sizeof(coords) / sizeof(coords[0]) withDuration:_duration withName:nil completeCallback:^(BOOL isFinished) {
if ([[dict[@"v"] stringValue] isEqualToString:@"0"] || i == _data.count) {
WeakAnno.title = @"停留";
WeakAnno.subtitle = [NSString stringWithFormat:@"%@ s", [self timeExchange:dict[@"t"]]];
}else{
WeakAnno.title = [NSString stringWithFormat:@"%@ (km/h)", dict[@"v"]];
WeakAnno.subtitle = [NSString stringWithFormat:@"%@ s", [self timeExchange:dict[@"t"]]];
}
网友评论