RoutePath-(高德地图)根据定位点绘制路线+轨迹纠偏

轨迹纠偏的作用就是去掉绘制路线时候两个定位点之间产生的毛刺和尖角,使路线看起来更加的圆滑,正常

RoutePath-(高德地图)根据定位点绘制路线+轨迹纠偏_第1张图片
IMG_0323.PNG
- (void) DrawLine{
    
    CLLocationCoordinate2D coordinate;
    CLLocation *location;
    NSMutableArray * array = [NSMutableArray array];
    for (int i = 0; i < self.locations.count; i++) {
        
        coordinate.latitude = [self.latitudes[i] floatValue];
        coordinate.longitude = [self.longitudes[i] floatValue];
        
        location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        
        [self.tempTraceLocations addObject:location];

        
        [array addObject:location];
    }

}

- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id )overlay
{
    if ([overlay isKindOfClass:[MAPolyline class]])
    {
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
        
        polylineRenderer.lineWidth    = 4.0f;
        polylineRenderer.strokeColor  = KMainColor;
        polylineRenderer.lineJoinType = kMALineJoinRound;
        polylineRenderer.lineCapType  = kMALineCapRound;
        
        return polylineRenderer;
    }
    
    return nil;
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation
{

    if ([annotation isKindOfClass:[MAPointAnnotation class]]){
        static NSString *pointReuseIndetifier = @"driverReuseIndetifier";
        
        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        
        if (annotationView == nil){
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:pointReuseIndetifier];
        }
        if (![annotation isKindOfClass:[MANaviAnnotation class]])
        {
            /* 起点. */
            if ([[annotation title] isEqualToString:(NSString*)RoutePlanningViewControllerStartTitle])
            {
                annotationView.image = [UIImage imageNamed:@"起点icon"];
                //设置中心点偏移,使得标注底部中间点成为经纬度对应点
                annotationView.centerOffset = CGPointMake(0, -(CURRENT_SIZE(24)/2));

            }
            /* 终点. */
            else if([[annotation title] isEqualToString:(NSString*)RoutePlanningViewControllerDestinationTitle])
            {
                annotationView.image = [UIImage imageNamed:@"终点icon"];
                //设置中心点偏移,使得标注底部中间点成为经纬度对应点
                annotationView.centerOffset = CGPointMake(0, -(CURRENT_SIZE(24)/2));

            }
        }
        return annotationView;
    }
    
    
    return nil;
}
#pragma mark -------------------- 轨迹纠偏 --------------------
- (void)queryTraceWithLocations:(NSArray *)locations withSaving:(BOOL)saving
{
    NSMutableArray *mArr = [NSMutableArray array];
    for(CLLocation *loc in locations)
    {
        MATraceLocation *tLoc = [[MATraceLocation alloc] init];
        tLoc.loc = loc.coordinate;
        
        tLoc.speed = loc.speed * 3.6; //m/s  转 km/h
        tLoc.time = [loc.timestamp timeIntervalSince1970] * 1000;
        tLoc.angle = loc.course;
        [mArr addObject:tLoc];
    }
    
    __weak typeof(self) weakSelf = self;
    __unused NSOperation *op = [self.traceManager queryProcessedTraceWith:mArr type:-1 processingCallback:nil  finishCallback:^(NSArray *points, double distance) {
        
        NSLog(@"trace query done!");
        [weakSelf addFullTrace:points];

        
    } failedCallback:^(int errorCode, NSString *errorDesc) {
        
        NSLog(@"Error: %@", errorDesc);
//        weakSelf.queryOperation = nil;
    }];
    
}

GitHub代码下载地址
喜欢的记得给个Star

我是Renjiee 我要做最骚的程序猿‍‍

你可能感兴趣的:(RoutePath-(高德地图)根据定位点绘制路线+轨迹纠偏)