|
@@ -7,10 +7,27 @@ export class GetRouterService {
|
|
|
|
|
|
async searchRoad(appId, startPointId, clicking_point) {
|
|
|
//表示终点
|
|
|
- const endPoint = {
|
|
|
- position:clicking_point,
|
|
|
- breakPointId:-100
|
|
|
+ let endPoint;
|
|
|
+
|
|
|
+ const keys = await this.cacheService.keys(`breakpoints:app_id:${appId}*`);
|
|
|
+ let minDis = null;
|
|
|
+ for (let i = 0; i < keys.length; ++i) {
|
|
|
+ const breakPointRes = await this.cacheService.get(keys[i]);
|
|
|
+ const breakPoint = JSON.parse(breakPointRes);
|
|
|
+ const position = breakPoint.position;
|
|
|
+ if(minDis == null){
|
|
|
+ minDis = this.getDistance(clicking_point,position);
|
|
|
+ endPoint = breakPoint;
|
|
|
+ }
|
|
|
+ else if(minDis>this.getDistance(clicking_point,position)){
|
|
|
+ endPoint = breakPoint;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(minDis>100){
|
|
|
+ return [];
|
|
|
}
|
|
|
+
|
|
|
const startPointRes = await this.cacheService.get(
|
|
|
'breakpoints:app_id:' + appId + ':break_point_id:' + startPointId,
|
|
|
);
|
|
@@ -21,10 +38,10 @@ export class GetRouterService {
|
|
|
// );
|
|
|
// const endPoint = JSON.parse(endPointRes);
|
|
|
|
|
|
- let openList = [], //开启列表
|
|
|
- closeList = [], //关闭列表
|
|
|
- result = [], //结果数组
|
|
|
- result_index; //结果数组在开启列表中的序号
|
|
|
+ const openList = [], //开启列表
|
|
|
+ closeList = []; //关闭列表
|
|
|
+ let result = []; //结果数组
|
|
|
+ let result_index: number; //结果数组在开启列表中的序号
|
|
|
|
|
|
//openList.push({x:startPoint.x,y:startPoint.y,G:0});//把当前点加入到开启列表中,并且G是0
|
|
|
openList.push({ breakPointId: startPointId, G: 0 }); //把当前点加入到开启列表中,并且G是0
|
|
@@ -38,23 +55,30 @@ export class GetRouterService {
|
|
|
currentPointInfo.breakPointId,
|
|
|
);
|
|
|
const currentPoint = JSON.parse(currentPointRes);
|
|
|
-
|
|
|
closeList.push(currentPointInfo);
|
|
|
- const surroundPoint = this.SurroundPoint(
|
|
|
- appId,
|
|
|
- currentPointInfo.breakPointId,
|
|
|
- endPoint
|
|
|
+
|
|
|
+ //读redis里的数据
|
|
|
+ const breakPointRes = await this.cacheService.get(
|
|
|
+ 'breakpoints:app_id:' +
|
|
|
+ appId +
|
|
|
+ ':break_point_id:' +
|
|
|
+ currentPointInfo.breakPointId,
|
|
|
);
|
|
|
- for (const i in surroundPoint) {
|
|
|
+ let surroundPoint = [];
|
|
|
+ const _breakPoint = JSON.parse(breakPointRes);
|
|
|
+ surroundPoint = _breakPoint.contact;
|
|
|
+
|
|
|
+ for (let i = 0; i < surroundPoint.length; ++i) {
|
|
|
const neighPointId = surroundPoint[i];
|
|
|
const itemRes = await this.cacheService.get(
|
|
|
'breakpoints:app_id:' + appId + ':break_point_id:' + neighPointId,
|
|
|
);
|
|
|
- const item = JSON.parse(itemRes);
|
|
|
+ let item = JSON.parse(itemRes);
|
|
|
//g 到父节点的位置
|
|
|
- const g = currentPointInfo.G + this.getDistance(currentPoint.position,item.position)
|
|
|
-
|
|
|
- if (this.existList(item, openList) != -1) {
|
|
|
+ const g =
|
|
|
+ currentPointInfo.G +
|
|
|
+ this.getDistance(currentPoint.position, item.position);
|
|
|
+ if (this.existList(item, openList) == -1) {
|
|
|
//如果不在开启列表中
|
|
|
item['H'] = 0;
|
|
|
item['G'] = g;
|
|
@@ -77,12 +101,10 @@ export class GetRouterService {
|
|
|
break;
|
|
|
}
|
|
|
openList.sort(this.sortF); //这一步是为了循环回去的时候,找出 F 值最小的, 将它从 "开启列表" 中移掉
|
|
|
- } while (
|
|
|
- !(result_index = this.existList(endPoint,openList))
|
|
|
- );
|
|
|
+ } while ((result_index = this.existList(endPoint, openList))==-1);
|
|
|
|
|
|
//判断结果列表是否为空
|
|
|
- if (!result_index) {
|
|
|
+ if (result_index == -1) {
|
|
|
result = [];
|
|
|
} else {
|
|
|
let currentObj = openList[result_index];
|
|
@@ -95,7 +117,8 @@ export class GetRouterService {
|
|
|
currentObj.position.y != startPoint.position.y
|
|
|
);
|
|
|
}
|
|
|
- debugger
|
|
|
+ debugger;
|
|
|
+ result.unshift(startPointId);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -105,13 +128,13 @@ export class GetRouterService {
|
|
|
}
|
|
|
|
|
|
//获取周围点
|
|
|
- async SurroundPoint(appId, curPointId,endPoint) {
|
|
|
+ async SurroundPoint(appId, curPointId, endPoint) {
|
|
|
//读redis里的数据
|
|
|
const breakPointRes = await this.cacheService.get(
|
|
|
'breakpoints:app_id:' + appId + ':break_point_id:' + curPointId,
|
|
|
);
|
|
|
const breakPoint = JSON.parse(breakPointRes);
|
|
|
- if(this.getDistance(breakPoint.position,endPoint.position)<1){
|
|
|
+ if (this.getDistance(breakPoint.position, endPoint.position) < 1) {
|
|
|
breakPoint.contact.push(endPoint.breakPointId);
|
|
|
}
|
|
|
return breakPoint.contact;
|
|
@@ -127,7 +150,10 @@ export class GetRouterService {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- getDistance(position1,position2){
|
|
|
- return Math.sqrt((position1.x - position2.x)*(position1.x - position2.x)+(position1.y - position2.y)*(position1.y - position2.y))
|
|
|
+ getDistance(position1, position2) {
|
|
|
+ return Math.sqrt(
|
|
|
+ (position1.x - position2.x) * (position1.x - position2.x) +
|
|
|
+ (position1.y - position2.y) * (position1.y - position2.y),
|
|
|
+ );
|
|
|
}
|
|
|
}
|