美文网首页
高德 模拟小车实时移动

高德 模拟小车实时移动

作者: yuezishenyou | 来源:发表于2019-01-18 14:16 被阅读0次

需求: 业务当司机接到乘客后,有一个规划的路线,然后司机前往目的地,小车走过的路线置成灰色。

思路:

  1. 根据上下车点,进行驾车路线规划,并画线
    2.实时获取经纬度点。(经纬度点要验证,筛选)
    3.获取的点,进行纠偏处理。
    4.纠偏后的点,是小车移动的重要组成部分,小车移动中画走过的线。

这里是示例demo

重要代码块1.

驾车路线规划成功后画线 (画蓝线)
- (void)showNaviRoutes:(AMapNaviDriveManager *)driveManager
{
    if ([driveManager.naviRoutes count] <= 0) {
        return;
    }
    [kMAPMANAGER.mapView removeOverlays:kMAPMANAGER.mapView.overlays];
    AMapNaviRoute *aRoute = driveManager.naviRoute;
    int count = (int)[[aRoute routeCoordinates] count];
    //添加路径Polyline
    CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)malloc(count * sizeof(CLLocationCoordinate2D));
    for (int i = 0; i < count; i++) {
        AMapNaviPoint *coordinate = [[aRoute routeCoordinates] objectAtIndex:i];
        coords[i].latitude = [coordinate latitude];
        coords[i].longitude = [coordinate longitude];
    }
    self.fullTraceLine = [MAPolyline polylineWithCoordinates:coords count:count];
    [kMAPMANAGER.mapView addOverlay:self.fullTraceLine];
    free(coords);   
}

重要代码块2.

#pragma mark --新的定位或者服务器推给你的定位
- (void)updateLocation:(CLLocation *)location {
    
    CLLocationCoordinate2D coordinate = location.coordinate;
    if (coordinate.latitude <= 0 || coordinate.longitude <= 0) {
        return;
    }
    if (location.horizontalAccuracy < 100 && location.horizontalAccuracy > 0)
    {
        double lastDis = [location distanceFromLocation:self.currentRecord.endLocation];
        NSLog(@"---------这个距离:(%.2f)-----------",lastDis);
        
        if (lastDis < 0.0 || lastDis > 10) {
            
            [self.currentRecord addLocation:location];
            
            // 其实这里可以置小车为中间点 也可以画线
            //[kMAPMANAGER.mapView setCenterCoordinate:location.coordinate animated:YES];
        
            [self.tempTraceLocations addObject:location];
            if (self.tempTraceLocations.count >= kTempTraceLocationCount) {
                NSLog(@"---------开始纠偏----------");
                [self queryTraceWithLocations:self.tempTraceLocations withSaving:NO];
                [self.tempTraceLocations removeAllObjects];
                
                // 把最后一个再add一遍,否则会有缝隙
                [self.tempTraceLocations addObject:location];
            }
        }
    }
}


#pragma mark --纠偏处理
- (void)queryTraceWithLocations:(NSArray <CLLocation *>*)locations withSaving:(BOOL)saving
{
    if (locations.count < 2) {
        return;
    }
    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<MATracePoint *> *points, double distance) {
        
        NSLog(@"--------纠偏完成:%ld---------",points.count);
        [weakSelf updateUserlocationTitleWithDistance:distance];
        [weakSelf addFullTrace:points];
        
    } failedCallback:^(int errorCode, NSString *errorDesc) {
        
        NSLog(@"--------纠偏失败:%@---------",errorDesc);
        
    }];   
}

// 小车将要平滑移动的点
- (void)addFullTrace:(NSArray<MATracePoint*> *)tracePoints {
    
    NSInteger count = tracePoints.count;
    if (count < 2) {
        return ;
    }
    CLLocationCoordinate2D *pCoords = malloc(sizeof(CLLocationCoordinate2D) * count);
    if (!pCoords) {
        return;
    }
    NSMutableArray *points = [NSMutableArray array];
    for (int i = 0; i < count; ++i) {
        //1.结构体
        MATracePoint *p = [tracePoints objectAtIndex:i];
        CLLocationCoordinate2D *pCur = pCoords + i;
        pCur->latitude = p.latitude;
        pCur->longitude = p.longitude;
        //2.经纬度数组
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:p.latitude longitude:p.longitude];
        [points addObject:loc];
    }
    
    //把这个点给带小车的地图页面
    if ([self.delegate respondsToSelector:@selector(locations:points:moveWithCoordinates:count:WithDuration:)]) {
        [self.delegate locations:self points:points moveWithCoordinates:pCoords count:count WithDuration:count];
    }
    if (pCoords) {
        free(pCoords);
    }
}


// 距离
- (void)updateUserlocationTitleWithDistance:(double)distance {
      //self.totalTraceLength += distance; //
}

重要代码块3.

#pragma mark ------------------------------- 定位加工 代理 -----------------------------------------
- (void)locations:(JBKLocations *)locations points:(NSArray *)points moveWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSInteger)count WithDuration:(CGFloat)duration {
    
    DLog(@"--------来了老弟------------\n");
    
    [self.allPoints addObjectsFromArray:points];
    NSInteger totalCount = self.allPoints.count;
    self.all_coords = malloc(sizeof(CLLocationCoordinate2D) * totalCount);
    for (int i = 0; i < totalCount; i++) {
        CLLocation *loc = self.allPoints[i];
        self.all_coords[i] = loc.coordinate;
    }
    
    __weak typeof(self) weakSelf = self;
    double speed = 120.0 / 3.6;
    float  dis = 120.0;
    [self.carAnnotation addMoveAnimationWithKeyCoordinates:coords count:count withDuration:(dis / speed) withName:nil completeCallback:^(BOOL isFinished) {
        weakSelf.passedTraceCoordIndex += count;
        weakSelf.title = [NSString stringWithFormat:@"小车移动(%ld)",(long)weakSelf.passedTraceCoordIndex];
    }];   
}


- (void)updateTracePassed
{
    if(self.carAnnotation.isAnimationFinished) {
        return;
    }

    if(self.passedTraceLine) {
        [kMAPMANAGER.mapView removeOverlay:self.passedTraceLine];
    }

    // 
    NSInteger needCount = self.passedTraceCoordIndex + 2;
    CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * needCount);
    memcpy(coords, self.all_coords, sizeof(CLLocationCoordinate2D) * (self.passedTraceCoordIndex + 1));
    coords[needCount - 1] = self.carAnnotation.coordinate;
    self.passedTraceLine = [MAPolyline polylineWithCoordinates:coords count:needCount];
    [kMAPMANAGER.mapView addOverlay:self.passedTraceLine];
    free(coords);

#if 0
    NSInteger totalCount = self.allPoints.count;
    self.passedTraceLine = [MAPolyline polylineWithCoordinates:self.all_coords count:totalCount];
    [kMAPMANAGER.mapView addOverlay:self.passedTraceLine];
#endif
    
    
}

相关文章

网友评论

      本文标题:高德 模拟小车实时移动

      本文链接:https://www.haomeiwen.com/subject/aurndqtx.html