635 lines
12 KiB
JavaScript
635 lines
12 KiB
JavaScript
import {
|
||
showModelMessage
|
||
} from "./tools.js";
|
||
import * as api from "./api.js";
|
||
import config from "./config.js";
|
||
|
||
const imgPath = config.imgPath;
|
||
|
||
export const sdkKey = '1f8f159583f28dfba6eb06deb55e3b56';
|
||
|
||
export const getMap = (mapid, instance) => {
|
||
return uni.createMapContext(mapid, {
|
||
this: instance.proxy
|
||
});
|
||
}
|
||
//获取当前定位
|
||
export const getLoalcation = (oMap, success, fail, nomove) => {
|
||
uni.getLocation({
|
||
type: 'gcj02', // 返回可以用于 wx.openLocation 的经纬度
|
||
geocode: true,
|
||
success(res) {
|
||
const {
|
||
latitude,
|
||
longitude
|
||
} = res;
|
||
if (!nomove) {
|
||
moveToLocation(oMap, longitude, latitude);
|
||
}
|
||
// if (markers) { //定位
|
||
// const arrData = markers.value || [];
|
||
// let index = arrData.findIndex((item) => {
|
||
// return item.id == 0;
|
||
// });
|
||
// index = index == -1 ? arrData.length : index;
|
||
// arrData[index] = addMarker(0, longitude, latitude, "dw.png");
|
||
// markers.value = arrData;
|
||
// }
|
||
if (success) success({
|
||
latitude,
|
||
longitude
|
||
});
|
||
},
|
||
fail(res) {
|
||
if (fail) fail(res);
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
//逆解析地址
|
||
export const reverseGeocoder = (mapsdk, longitude, latitude, success, fail) => {
|
||
|
||
mapsdk.getRegeo({
|
||
location: `${longitude},${latitude}`,
|
||
success: function(res) {
|
||
const {
|
||
name,
|
||
regeocodeData
|
||
} = res[0];
|
||
const {
|
||
addressComponent: {
|
||
adcode,
|
||
province,
|
||
city,
|
||
district
|
||
},
|
||
formatted_address
|
||
} = regeocodeData
|
||
if (success) success({
|
||
name,
|
||
formatted_address,
|
||
adcode,
|
||
province,
|
||
city,
|
||
district
|
||
});
|
||
},
|
||
fail: function(info) {
|
||
if (fail) fail(res);
|
||
}
|
||
})
|
||
// mapsdk.reverseGeocoder({
|
||
// location: {
|
||
// latitude: latitude,
|
||
// longitude: longitude
|
||
// },
|
||
// success: (res) => {
|
||
// if (success) success(res.result);
|
||
// },
|
||
// fail: (res) => {
|
||
// if (fail) fail(res);
|
||
// }
|
||
// })
|
||
}
|
||
|
||
//添加点
|
||
export const addMarker = (id, longitude, latitude, icon) => {
|
||
return {
|
||
id: id,
|
||
latitude,
|
||
longitude,
|
||
width: 40,
|
||
height: 40,
|
||
zIndex: "100",
|
||
iconPath: imgPath + "static/map/" + icon
|
||
}
|
||
}
|
||
|
||
//添加线
|
||
export const addLine = (scolor, arrPoint) => {
|
||
// let scolor = "";
|
||
// switch (lx) {
|
||
// case "add":
|
||
// scolor = "#e50413";
|
||
// break;
|
||
// case "edit":
|
||
// scolor = "#e58b04";
|
||
// break;
|
||
// default:
|
||
// scolor = "#0473e5";
|
||
// break
|
||
// }
|
||
return {
|
||
points: arrPoint,
|
||
color: scolor,
|
||
width: 4
|
||
}
|
||
}
|
||
|
||
//添加面
|
||
export const addPolygon = (scolor, fcolor, arrPolygonsPoits) => {
|
||
/* let scolor = "",
|
||
fcolor = "";
|
||
switch (lx) {
|
||
case "add":
|
||
scolor = "#e50413";
|
||
fcolor = "#f79bac73";
|
||
break;
|
||
case "edit":
|
||
scolor = "#e58b04";
|
||
fcolor = "#e58b0440";
|
||
break;
|
||
default:
|
||
scolor = "#0473e5";
|
||
fcolor = "#0473e540";
|
||
break
|
||
} */
|
||
return {
|
||
points: arrPolygonsPoits,
|
||
fillColor: fcolor,
|
||
strokeWidth: 3,
|
||
strokeColor: scolor,
|
||
zIndex: 11
|
||
}
|
||
}
|
||
|
||
//添加圆
|
||
export const addCirle = (scolor, fcolor, longitude, latitude, radius) => {
|
||
/* let scolor = "",
|
||
fcolor = "";
|
||
switch (lx) {
|
||
case "add":
|
||
scolor = "#e50413";
|
||
fcolor = "#f79bac73";
|
||
break;
|
||
case "edit":
|
||
scolor = "#e58b04";
|
||
fcolor = "#e58b0440";
|
||
break;
|
||
default:
|
||
scolor = "#0473e5";
|
||
fcolor = "#0473e540";
|
||
break
|
||
} */
|
||
return {
|
||
latitude,
|
||
longitude,
|
||
radius,
|
||
color: scolor,
|
||
fillColor: fcolor,
|
||
strokeWidth: 3
|
||
}
|
||
}
|
||
|
||
//绘制多边形
|
||
export const drawPolygons = (obj, e) => {
|
||
/* const {
|
||
detail: {
|
||
latitude,
|
||
longitude
|
||
}
|
||
} = e;
|
||
let {
|
||
markers,
|
||
polylines,
|
||
polygons,
|
||
arrPolygonsPoits,
|
||
datacicle,
|
||
circles
|
||
} = obj
|
||
if (datacicle) { //移除圆
|
||
obj.datacicle = null;
|
||
obj.circles.splice(circles.length - 1, 1);
|
||
}
|
||
const len = arrPolygonsPoits.length;
|
||
arrPolygonsPoits.push({
|
||
latitude,
|
||
longitude
|
||
});
|
||
switch (len) {
|
||
case 0:
|
||
const marker = addMarker("add", 10000, latitude, longitude);
|
||
markers.push(marker);
|
||
break;
|
||
case 1:
|
||
polylines.push(addLine("add", arrPolygonsPoits));
|
||
break;
|
||
case 2:
|
||
obj.polylines = [];
|
||
polygons.push(addPolygon("add", arrPolygonsPoits));
|
||
break;
|
||
default:
|
||
polygons[polygons.length - 1] = addPolygon("add", arrPolygonsPoits);
|
||
console.log("333333333333", JSON.stringify(arrPolygonsPoits));
|
||
break;
|
||
} */
|
||
}
|
||
|
||
//移至中心点
|
||
export const moveToLocation = (oMap, longitude, latitude) => {
|
||
const point = {
|
||
latitude,
|
||
longitude
|
||
}
|
||
oMap.moveToLocation(point);
|
||
}
|
||
|
||
//运营区
|
||
export function getOperation(zoneId, callback) {
|
||
const params = {
|
||
zoneId
|
||
}
|
||
api.callOperateApi("ebikeRegion/getOperation", params, "get").then((res) => {
|
||
const {
|
||
code,
|
||
message,
|
||
data
|
||
} = res;
|
||
if (code != 200) {
|
||
showModelMessage(message);
|
||
if (callback) callback(null);
|
||
return;
|
||
}
|
||
const arrCircles = [];
|
||
const arrPolygons = [];
|
||
const arrRegionID = [];
|
||
const arrCirclesData = [];
|
||
const arrPolygonsData = [];
|
||
data.map((item, index) => {
|
||
const {
|
||
shapeType,
|
||
operationRegionId: regionId,
|
||
radius,
|
||
points
|
||
} = item;
|
||
if (!points) {
|
||
console.log("getOperation坐标点未返回");
|
||
return;
|
||
}
|
||
arrRegionID.push(regionId);
|
||
const scolor = "#578FD4";
|
||
const fcolor = "#c0daf54d";
|
||
if (shapeType == 1) {
|
||
const {
|
||
longitude: lng,
|
||
latitude: lat
|
||
} = points[0];
|
||
arrCirclesData.push(item);
|
||
arrCircles.push(addCirle(scolor, fcolor, lng, lat, radius));
|
||
} else if (shapeType == 2) {
|
||
const arrPoint = points.map(p => {
|
||
return {
|
||
longitude: p.longitude,
|
||
latitude: p.latitude
|
||
}
|
||
})
|
||
arrPolygonsData.push(item);
|
||
arrPolygons.push(addPolygon(scolor, fcolor, arrPoint));
|
||
}
|
||
|
||
});
|
||
|
||
if (callback) {
|
||
const resData = {
|
||
arrRegionID,
|
||
arrCircles,
|
||
arrPolygons,
|
||
arrData: data,
|
||
arrCirclesData,
|
||
arrPolygonsData
|
||
}
|
||
callback(resData);
|
||
}
|
||
|
||
})
|
||
};
|
||
|
||
//站点、停车区
|
||
export function getRegionData(arrRegionID, callback) {
|
||
if (!arrRegionID || arrRegionID.length == 0) return;
|
||
const arrMethods = [];
|
||
arrRegionID.map(regionId => {
|
||
const params = {
|
||
regionId
|
||
};
|
||
arrMethods.push(api.callOperateApi("ebikeRegion/getRegion", params, "get"));
|
||
});
|
||
|
||
Promise.all(arrMethods).then(res => {
|
||
const arrCircles = [];
|
||
const arrPolygons = [];
|
||
const arrData = [];
|
||
const arrCirclesData = [];
|
||
const arrPolygonsData = [];
|
||
res.map(item => {
|
||
const {
|
||
code,
|
||
message,
|
||
data: arrRegion
|
||
} = item;
|
||
if (code != 200) return;
|
||
arrRegion.map(data => {
|
||
const {
|
||
regionId,
|
||
shapeType,
|
||
radius,
|
||
points
|
||
} = data;
|
||
arrData.push(data);
|
||
const scolor = "#58d4c0";
|
||
const fcolor = "#c1f5f138";
|
||
if (shapeType == 1) {
|
||
const {
|
||
longitude: lng,
|
||
latitude: lat
|
||
} = points[0];
|
||
arrCircles.push(addCirle(scolor, fcolor, lng, lat, radius));
|
||
arrCirclesData.push(data);
|
||
} else if (shapeType == 2) {
|
||
if (!points) return;
|
||
const arrPoint = points.map(p => {
|
||
return {
|
||
longitude: p.longitude,
|
||
latitude: p.latitude
|
||
}
|
||
})
|
||
arrPolygons.push(addPolygon(scolor, fcolor, arrPoint));
|
||
arrPolygonsData.push(data);
|
||
}
|
||
})
|
||
})
|
||
if (callback) callback({
|
||
arrData,
|
||
arrCircles,
|
||
arrPolygons,
|
||
arrCirclesData,
|
||
arrPolygonsData
|
||
});
|
||
})
|
||
}
|
||
|
||
//获取车辆数据
|
||
export const getBikeData = (arrRegionID, callback) => {
|
||
if (!arrRegionID || arrRegionID.length == 0) return;
|
||
const arrMethods = [];
|
||
const params = {
|
||
operationRegionIds: arrRegionID
|
||
}
|
||
api.callMaintenanceApi("operation/list", params, "post").then(res => {
|
||
const arrPoints = [];
|
||
const arrData = [];
|
||
let icnt = 0;
|
||
let icnt_0 = 0;
|
||
let icnt_2 = 0;
|
||
let {
|
||
code,
|
||
message,
|
||
data
|
||
} = res;
|
||
if (code != 200) {
|
||
showModelMessage(message);
|
||
} else {
|
||
if (data.length > 0) arrData.push(...data);
|
||
|
||
}
|
||
callback({
|
||
arrData
|
||
});
|
||
})
|
||
// api.callMaintenanceApi("operation/list",params,"get").then(res=>{
|
||
// console.log("5555555555555555555555555",res);
|
||
// });
|
||
/* arrRegionID.map(regionId => {
|
||
const params = {
|
||
regionId
|
||
};
|
||
arrMethods.push(api.callOperateApi("ebikeRegion/getOperationBike", params, "get"));
|
||
}); */
|
||
/* Promise.all(arrMethods).then(arrRes => {
|
||
const arrPoints = [];
|
||
const arrData = [];
|
||
let icnt = 0;
|
||
let icnt_0 = 0;
|
||
let icnt_2 = 0;
|
||
arrRes.map(res => {
|
||
let {
|
||
code,
|
||
message,
|
||
data
|
||
} = res;
|
||
if (code != 200) {
|
||
showModelMessage(message);
|
||
} else {
|
||
if (data.length > 0) arrData.push(...data);
|
||
|
||
}
|
||
});
|
||
callback({
|
||
arrData
|
||
});
|
||
}) */
|
||
|
||
}
|
||
|
||
function testData() {
|
||
const points = []
|
||
const centerLng = 103.969028;
|
||
const centerLat = 30.632034;
|
||
const count = 1000;
|
||
|
||
for (let i = 0; i < count; i++) {
|
||
// 在中心点附近随机生成点
|
||
const lng = centerLng + (Math.random() - 0.5) * 0.02
|
||
const lat = centerLat + (Math.random() - 0.5) * 0.02
|
||
const data = {
|
||
bikeCode: i,
|
||
bikeId: i,
|
||
longitude: lng,
|
||
latitude: lat,
|
||
soc: Math.floor(Math.random() * 100)
|
||
}
|
||
points.push(data);
|
||
}
|
||
return points;
|
||
}
|
||
|
||
//电量车辆点
|
||
export function addMarker_Q(id, data, joinCluster, lx) {
|
||
let {
|
||
longitude,
|
||
latitude,
|
||
soc,
|
||
inOperation,
|
||
inParking
|
||
} = data;
|
||
let icon = "";
|
||
let lbContent = "";
|
||
switch (lx) {
|
||
case "hd":
|
||
icon = "bike_1.png";
|
||
lbContent = soc + "%";
|
||
if (soc <= 20) {
|
||
icon = "bike_0.png";
|
||
} else if (soc <= 60) {
|
||
icon = "bike_2.png";
|
||
}
|
||
break;
|
||
case "dd":
|
||
icon = "bike_0.png";
|
||
if(inOperation){
|
||
lbContent="超区";
|
||
}
|
||
else if(inParking){
|
||
lbContent="违停";
|
||
}
|
||
break;
|
||
case "wx":
|
||
break;
|
||
}
|
||
|
||
const systemInfo = uni.getSystemInfoSync();
|
||
|
||
let oLabel = {
|
||
content: lbContent,
|
||
color: "#FFF",
|
||
anchorY: -25,
|
||
fontSize: 9,
|
||
textAlign: "center",
|
||
};
|
||
// 判断设备类型(iOS/Android)
|
||
if (systemInfo.platform === 'android') {
|
||
oLabel = {
|
||
...oLabel,
|
||
anchorX: -6
|
||
}
|
||
}
|
||
return {
|
||
id: id,
|
||
latitude,
|
||
longitude,
|
||
width: 30,
|
||
height: 30,
|
||
iconPath: "/static/map/" + icon,
|
||
joinCluster: joinCluster,
|
||
label: oLabel
|
||
}
|
||
}
|
||
|
||
|
||
//导航
|
||
export function direction({
|
||
mapsdk,
|
||
slng,
|
||
slat,
|
||
elng,
|
||
elat,
|
||
success,
|
||
fail
|
||
}) {
|
||
const options = {
|
||
origin: `${slng},${slat}`,
|
||
destination: `${elng},${elat}`,
|
||
success: (res) => {
|
||
let points = [];
|
||
let distance = 0;
|
||
if (res.paths && res.paths[0] && res.paths[0].steps) {
|
||
let steps = res.paths[0].steps;
|
||
distance = res.paths[0].distance;
|
||
for (let i = 0; i < steps.length; i++) {
|
||
let poLen = steps[i].polyline.split(';');
|
||
for (let j = 0; j < poLen.length; j++) {
|
||
points.push({
|
||
longitude: parseFloat(poLen[j].split(',')[0]),
|
||
latitude: parseFloat(poLen[j].split(',')[1])
|
||
})
|
||
}
|
||
}
|
||
}
|
||
const cnt = (distance / 1000).toFixed(2);
|
||
let sdistance = distance + "m";
|
||
if (cnt >= 1) {
|
||
sdistance = cnt + "km";
|
||
}
|
||
const data = {
|
||
arrPoint: points,
|
||
distance: sdistance
|
||
}
|
||
if (success) success(data);
|
||
},
|
||
fail: (res) => {
|
||
if (fail) fail(res);
|
||
}
|
||
}
|
||
mapsdk.getDrivingRoute(options)
|
||
// const options = {
|
||
// from: {
|
||
// longitude: slng,
|
||
// latitude: slat
|
||
// },
|
||
// to: {
|
||
// longitude: elng,
|
||
// latitude: elat
|
||
// },
|
||
// success: (res) => {
|
||
// const {
|
||
// polyline,
|
||
// distance
|
||
// } = res.result.routes[0];
|
||
// let coors = polyline;
|
||
// for (let i = 2; i < coors.length; i++) {
|
||
// coors[i] = coors[i - 2] + coors[i] / 1000000
|
||
// }
|
||
// let arrPoint = [];
|
||
// for (let i = 0; i < coors.length; i += 2) {
|
||
// arrPoint.push({
|
||
// latitude: coors[i],
|
||
// longitude: coors[i + 1]
|
||
// })
|
||
// }
|
||
// const cnt = (distance / 1000).toFixed(2);
|
||
// let sdistance = distance + "m";
|
||
// if (cnt >= 1) {
|
||
// sdistance = cnt + "km";
|
||
// }
|
||
// const data = {
|
||
// arrPoint,
|
||
// distance: sdistance
|
||
// }
|
||
|
||
// if (success) success(data);
|
||
// },
|
||
// fail: (res) => {
|
||
// fail(res);
|
||
// }
|
||
// }
|
||
// mapsdk.direction(options);
|
||
}
|
||
|
||
|
||
// 判断点是否在多边形内(射线法)
|
||
export function checkPointInPolygon(longitude, latitude, polygon) {
|
||
let x = longitude,
|
||
y = latitude;
|
||
let inside = false;
|
||
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
||
const xi = polygon[i].longitude,
|
||
yi = polygon[i].latitude;
|
||
const xj = polygon[j].longitude,
|
||
yj = polygon[j].latitude;
|
||
|
||
// 检测边是否跨过射线
|
||
const intersect = ((yi > y) !== (yj > y)) &&
|
||
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
|
||
if (intersect) inside = !inside;
|
||
}
|
||
return inside;
|
||
}
|
||
|
||
// 判断点(x,y)是否在圆心(cx,cy)半径r的圆内
|
||
export function isPointInCircle(longitude, latitude, center, radius) {
|
||
const dx = longitude - center.lng; // 经度差
|
||
const dy = latitude - center.lat; // 纬度差
|
||
const distance = Math.sqrt(dx * dx + dy * dy);
|
||
return distance <= radius;
|
||
} |