自定义室内地图以及路径规划

最近做到一个项目,设计到室内地图路径规划,其实一般的项目也很少设计到室内路径规划,室内也就那么点大。

但是上面怎么说我们就怎么做吧,或者是人性化,或者是多此一举的项目,既然写了就分享出来吧。

先说下大致思想流程吧,语言表达不是很好,有不懂的可以加我的qq24272779询问!

上图例子:

基本思路把上图建筑区域全部用坐标扣选出来,也就是不能走到的地方,蓝色区域和灰色区域。

坐标点以像素为单位。

扣选出来以后把整个图片地图分成由好多小方格组成的!

选择两个点,用A*算法求出需要经过非蓝色和灰色区域的最短路径。

(A*算法不懂的可以百度)。

代码:

//添加地图图片

 SKIndoorMapView *indoorMap = [[SKIndoorMapView alloc]initWithIndoorMapImageName:@"WHTerminalBD.png" Frame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:indoorMap];

//长安选择起点和终点

#pragma mark - Zoom methods
-(void)longRequired:(UIGestureRecognizer*)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        if (Points.count == 0 || Points.count >= 2)
        {
            [Points removeAllObjects];
            for (UIView *view in [self.mapView subviews])
            {
                [view removeFromSuperview];
            }
            //坐标
            CGPoint touchPoint = [gesture locationInView:self.mapView];
            UIImage *Img = [UIImage imageNamed:@"startPoint"];
            UIImageView *imgView = [[UIImageView alloc]initWithImage:Img];
            [imgView setFrame:CGRectMake(touchPoint.x - (Img.size.width / (self.zoomScale*2)), touchPoint.y - (Img.size.width / (self.zoomScale)), Img.size.width / self.zoomScale, Img.size.height / self.zoomScale)];
            [self.mapView addSubview:imgView];
            [Points addObject:NSStringFromCGPoint(touchPoint)];
        }
       else if (Points.count == 1)
        {
            //坐标
            CGPoint touchPoint = [gesture locationInView:self.mapView];
            UIImage *Img = [UIImage imageNamed:@"endPoint"];
            UIImageView *imgView = [[UIImageView alloc]initWithImage:Img];
            [imgView setFrame:CGRectMake(touchPoint.x - (Img.size.width / (self.zoomScale*2)), touchPoint.y - (Img.size.width / (self.zoomScale)), Img.size.width / self.zoomScale, Img.size.height / self.zoomScale)];
            [self.mapView addSubview:imgView];
            [Points addObject:NSStringFromCGPoint(touchPoint)];
            [self RoadRecevied:Points];
        }
       
    }

}

#pragma 路径
-(void)RoadRecevied:(NSMutableArray *)Arr
{
    /* 路径规划  */
    AStar *astar = [[AStar alloc]init];
    
    data = [astar findPath:CGPointFromString(Arr[0]).x curY:CGPointFromString(Arr[0]).y aimX:CGPointFromString(Arr[1]).x aimY:CGPointFromString(Arr[1]).y];
    /*
     size——同UIGraphicsBeginImageContext
     opaque—透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
     scale—–缩放因子
     */
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 480), NO, [UIScreen mainScreen].scale);
    UIBezierPath *path = [UIBezierPath bezierPath];
    [[UIColor redColor] setStroke];
    [path setLineWidth:0.5];
    [path setLineJoinStyle:kCGLineJoinRound];
    [path setLineCapStyle:kCGLineCapRound];
    BOOL isfirst = YES;
    if (data && [data count] > 0)
    {
        for (id obj in data)
        {
            if (isfirst)
            {
                isfirst = NO;
                [path moveToPoint:CGPointMake([obj id_col], [obj id_row])];//设置起始路径的点
            }
            [path addLineToPoint:CGPointMake([obj id_col], [obj id_row])];
        }
    }
    [path stroke];
    UIImage *img =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView *imgV = [[UIImageView alloc]initWithImage:img];
    [_mapView addSubview:imgV];
    [self setNeedsDisplay];
}

//这里我们来看看怎么扣选出蓝色区域和灰色非路径规划区域吧

<pre name="code" class="objc">- (bool)checkMap:(int)col row:(int)row
{
    CGPoint point = CGPointMake(col, row);
    
    /*
     
     150,45,180,45,180,70,150,70    值班室
     
     200,10,225,10,225,50,180,50,180,45,200,45    不明区域
     
     160,75,180,75,180,150,150,150,150,135,120,135,120,145,195,150,95,125,120,110,140,110,160,95    包裹打包区
     
     100,165,125,65,125,270,140,270,140,280,85,280,85,260,95,260,    候机大厅
     
     150,170,170,170,170,260,150,260   安检区
     
     10,310,60,280,150,290,150,275,180,275,180,305,150,305,150,360,170,360,170,390,200,405,200,475,150,475,150,380    休息区
     */
    
    /*
     180,350,220,250,220,370,180,380   不明区域
     
     0,0,200,0,200,45,150,45,150,75,10,145,60,170,70,28010,310,150,380,150,420,190,425,150,480,0,480   左边灰色
     
     230,0,320,0,320,480,230,480   右边灰色
     
     */
    
    NSString *obstacle00 = @"150,45,180,45,180,70,150,70";
    NSString *obstacle01 = @"200,10,225,10,225,50,180,50,180,45,200,45";
    NSString *obstacle02 = @"160,75,180,75,180,150,150,150,150,135,120,135,120,145,195,150,95,125,120,110,140,110,160,95";
    NSString *obstacle03 = @"100,165,125,65,125,270,140,270,140,280,85,280,85,260,95,260";
    NSString *obstacle04 = @"150,170,170,170,170,260,150,260 ";
    NSString *obstacle05 = @"10,310,60,280,150,290,150,275,180,275,180,305,150,305,150,360,170,360,170,390,200,405,200,475,150,475,150,380";
    NSString *obstacle06 = @"180,350,220,250,220,370,180,380";
    
    UIBezierPath  *path00 = [Utils bezierPathFromCoordinateString:obstacle00];
    UIBezierPath  *path01 = [Utils bezierPathFromCoordinateString:obstacle01];
    UIBezierPath  *path02 = [Utils bezierPathFromCoordinateString:obstacle02];
    UIBezierPath  *path03 = [Utils bezierPathFromCoordinateString:obstacle03];
    UIBezierPath  *path04 = [Utils bezierPathFromCoordinateString:obstacle04];
    UIBezierPath  *path05 = [Utils bezierPathFromCoordinateString:obstacle05];
    UIBezierPath  *path06 = [Utils bezierPathFromCoordinateString:obstacle06];
    
   // NSString *str110 = @"10,145,65,170,65,280,10,310,150,385,150,423,190,425,190,475,230,475,230,5,200,5,200,45,150,45,150,75";
    
        if (CGPathContainsPoint(path00.CGPath,NULL,point,false))
        {
            return NO;
        }
        else if (CGPathContainsPoint(path01.CGPath,NULL,point,false))
        {
            return NO;
        }
        else if (CGPathContainsPoint(path02.CGPath,NULL,point,false))
        {
            return NO;
        }
        else if (CGPathContainsPoint(path03.CGPath,NULL,point,false))
        {
            return NO;
        }
        else if (CGPathContainsPoint(path04.CGPath,NULL,point,false))
        {
            return NO;
        }
        else if (CGPathContainsPoint(path05.CGPath,NULL,point,false))
        {
            return NO;
        }
        else if (CGPathContainsPoint(path06.CGPath,NULL,point,false))
        {
            return NO;
        }
    return YES;
}

我们来看下最终效果!

转载自:https://mtr-1.oss-cn-beijing.aliyuncs.com/qyblog/2019/04/41693513.jpg

You may also like...