579 lines
12 KiB
JavaScript
Raw Normal View History

2025-04-23 16:03:22 +08:00
import {
showModelMessage
} from "./tools.js";
2025-04-14 10:57:27 +08:00
import * as api from "./api.js";
2025-04-15 19:03:32 +08:00
import config from "./config.js";
2025-04-14 10:57:27 +08:00
2025-04-23 16:03:22 +08:00
const imgPath = config.imgPath;
2025-04-14 10:57:27 +08:00
export const sdkKey = 'BECBZ-EJIEQ-LUU5N-B5ISQ-3TLMZ-BXFLG';
export const getMap = (mapid, instance) => {
return uni.createMapContext(mapid, {
this: instance.proxy
});
}
//获取当前定位
2025-04-29 16:36:28 +08:00
export const getLoalcation = (oMap, success, fail, nomove) => {
2025-04-14 10:57:27 +08:00
uni.getLocation({
type: 'gcj02', // 返回可以用于 wx.openLocation 的经纬度
geocode: true,
success(res) {
const {
latitude,
longitude
} = res;
2025-04-29 16:36:28 +08:00
if (!nomove) {
2025-04-23 16:03:22 +08:00
moveToLocation(oMap, longitude, latitude);
}
2025-04-14 10:57:27 +08:00
// 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 = (qqmapsdk, longitude, latitude, success, fail) => {
qqmapsdk.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude
},
success: (res) => {
if (success) success(res.result);
},
fail: (res) => {
2025-04-30 14:35:16 +08:00
if (fail) fail(res);
2025-04-14 10:57:27 +08:00
}
})
}
//添加点
export const addMarker = (id, longitude, latitude, icon) => {
return {
id: id,
latitude,
longitude,
2025-04-21 18:48:15 +08:00
width: 40,
height: 40,
2025-04-14 10:57:27 +08:00
zIndex: "100",
2025-04-23 16:03:22 +08:00
iconPath: imgPath + "static/map/" + icon
2025-04-14 10:57:27 +08:00
}
}
//添加线
2025-04-23 16:03:22 +08:00
export const addLine = (scolor, arrPoint) => {
// let scolor = "";
// switch (lx) {
// case "add":
// scolor = "#e50413";
// break;
// case "edit":
// scolor = "#e58b04";
// break;
// default:
// scolor = "#0473e5";
// break
// }
2025-04-14 10:57:27 +08:00
return {
2025-04-23 16:03:22 +08:00
points: arrPoint,
2025-04-14 10:57:27 +08:00
color: scolor,
2025-04-23 16:03:22 +08:00
width: 4
2025-04-14 10:57:27 +08:00
}
}
//添加面
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;
} */
}
//移至中心点
2025-04-23 16:03:22 +08:00
export const moveToLocation = (oMap, longitude, latitude) => {
2025-04-14 10:57:27 +08:00
const point = {
latitude,
longitude
}
oMap.moveToLocation(point);
}
2025-04-29 16:36:28 +08:00
//运营区
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);
2025-05-07 16:05:17 +08:00
if (callback) callback(null);
2025-04-29 16:36:28 +08:00
return;
}
const arrCircles = [];
const arrPolygons = [];
const arrRegionID = [];
2025-05-07 16:05:17 +08:00
const arrCirclesData = [];
2025-04-29 16:36:28 +08:00
const arrPolygonsData = [];
data.map((item, index) => {
const {
shapeType,
2025-05-07 16:05:17 +08:00
operationRegionId: regionId,
2025-04-29 16:36:28 +08:00
radius,
points
} = item;
2025-05-07 16:05:17 +08:00
if (!points) {
console.log("getOperation坐标点未返回");
return;
}
2025-04-29 16:36:28 +08:00
arrRegionID.push(regionId);
const scolor = "#578FD4";
2025-04-30 14:35:16 +08:00
const fcolor = "#c0daf54d";
2025-04-29 16:36:28 +08:00
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));
}
});
2025-05-07 16:05:17 +08:00
if (callback) {
const resData = {
arrRegionID,
arrCircles,
arrPolygons,
arrData: data,
arrCirclesData,
arrPolygonsData
}
console.log("000000000000000000000000", resData);
callback(resData);
}
2025-04-29 16:36:28 +08:00
})
};
2025-04-14 10:57:27 +08:00
//站点、停车区
2025-05-07 16:05:17 +08:00
export function getRegionData(arrRegionID, callback) {
2025-04-14 10:57:27 +08:00
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 = [];
2025-05-07 16:05:17 +08:00
const arrCirclesData = [];
2025-04-29 16:36:28 +08:00
const arrPolygonsData = [];
2025-04-14 10:57:27 +08:00
res.map(item => {
const {
code,
message,
2025-04-23 16:03:22 +08:00
data: arrRegion
2025-04-14 10:57:27 +08:00
} = item;
if (code != 200) return;
2025-04-23 16:03:22 +08:00
arrRegion.map(data => {
2025-04-14 10:57:27 +08:00
const {
regionId,
shapeType,
radius,
points
} = data;
arrData.push(data);
const scolor = "#58d4c0";
const fcolor = "#c1f5f1";
if (shapeType == 1) {
const {
longitude: lng,
latitude: lat
} = points[0];
2025-05-07 16:05:17 +08:00
arrCircles.push(addCirle(scolor, fcolor, lng, lat, radius));
2025-04-29 16:36:28 +08:00
arrCirclesData.push(data);
2025-04-14 10:57:27 +08:00
} else if (shapeType == 2) {
const arrPoint = points.map(p => {
return {
longitude: p.longitude,
latitude: p.latitude
}
})
arrPolygons.push(addPolygon(scolor, fcolor, arrPoint));
2025-04-29 16:36:28 +08:00
arrPolygonsData.push(data);
2025-04-14 10:57:27 +08:00
}
})
})
if (callback) callback({
arrData,
arrCircles,
2025-05-07 16:05:17 +08:00
arrPolygons,
arrCirclesData,
arrPolygonsData
2025-04-14 10:57:27 +08:00
});
})
}
//获取车辆数据
export const getBikeData = (arrRegionID, callback) => {
if (!arrRegionID || arrRegionID.length == 0) return;
const arrMethods = [];
arrRegionID.map(regionId => {
const params = {
regionId
};
arrMethods.push(api.callOperateApi("ebikeRegion/getOperationBike", params, "get"));
});
Promise.all(arrMethods).then(arrRes => {
2025-04-23 16:03:22 +08:00
const arrPoints = [];
const arrData = [];
let icnt = 0;
let icnt_0 = 0;
let icnt_2 = 0;
arrRes.map(res => {
const {
code,
message,
data
} = res;
if (code != 200) {
2025-04-14 10:57:27 +08:00
showModelMessage(message);
2025-04-23 16:03:22 +08:00
} else {
if (data.length > 0) arrData.push(...data);
data.map(item => {
const {
bikeId,
longitude,
latitude,
state,
soc
} = item;
const id = arrPoints.length;
if (soc <= 20) {
2025-04-21 18:48:15 +08:00
icnt_0++;
2025-04-23 16:03:22 +08:00
} else if (soc <= 60) {
2025-04-21 18:48:15 +08:00
icnt_2++;
2025-04-23 16:03:22 +08:00
} else {
2025-04-21 18:48:15 +08:00
icnt++;
}
2025-04-23 16:03:22 +08:00
arrPoints.push(addMarker_Q(id, item));
2025-04-14 10:57:27 +08:00
})
}
});
2025-04-23 16:03:22 +08:00
callback({
arrData,
arrPoints,
icnt,
icnt_0,
icnt_2
});
2025-04-14 10:57:27 +08:00
})
}
2025-04-21 18:48:15 +08:00
//电量车辆点
2025-04-23 16:03:22 +08:00
export function addMarker_Q(id, data) {
const {
longitude,
latitude,
soc
} = data;
let icon = "bike.png";
if (soc <= 20) {
icon = "bike_0.png";
} else if (soc <= 60) {
icon = "bike_2.png";
2025-04-21 18:48:15 +08:00
}
2025-04-23 16:03:22 +08:00
return {
2025-04-21 18:48:15 +08:00
id: id,
latitude,
longitude,
width: 40,
height: 40,
zIndex: "100",
2025-04-23 16:03:22 +08:00
iconPath: imgPath + "static/map/" + icon,
label: {
content: soc + "%",
color: "#FFF",
anchorY: -30,
fontSize: 9,
textAlign: "center",
2025-04-21 18:48:15 +08:00
}
}
}
2025-04-23 16:03:22 +08:00
//导航
export function direction({
qqmapsdk,
slng,
slat,
elng,
elat,
success,
fail
}) {
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";
2025-04-29 16:36:28 +08:00
if (cnt >= 1) {
2025-04-23 16:03:22 +08:00
sdistance = cnt + "km";
}
const data = {
arrPoint,
distance: sdistance
}
if (success) success(data);
},
fail: (res) => {
fail(res);
}
}
qqmapsdk.direction(options);
2025-04-29 16:36:28 +08:00
}
// 几何中心计算算法(适用于任意多边形)
2025-05-07 16:05:17 +08:00
export function getPolygonCenter(points) {
let X = 0,
Y = 0,
Z = 0;
const pointCount = points.length;
points.forEach(point => {
const lat = point.latitude * Math.PI / 180;
const lng = point.longitude * Math.PI / 180;
const x = Math.cos(lat) * Math.cos(lng);
const y = Math.cos(lat) * Math.sin(lng);
const z = Math.sin(lat);
X += x;
Y += y;
Z += z;
});
X /= pointCount;
Y /= pointCount;
Z /= pointCount;
const lng = Math.atan2(Y, X);
const lat = Math.atan2(Z, Math.sqrt(X * X + Y * Y));
return {
lat: lat * 180 / Math.PI,
lng: lng * 180 / Math.PI
};
}
// 工具函数:将像素坐标转换为经纬度
function pixelToLatLng(map, x, y) {
// return new Promise((resolve) => {
// map.getCenterLocation({
// success: (res) => {
// const center = { lat: res.latitude, lng: res.longitude };
// const scale = map.scale;
// const pixelWidth = 256 * Math.pow(2, (18 - scale)); // 缩放级别对应的像素宽度
// const latOffset = (y - map.height / 2) * (180 / (256 * Math.pow(2, scale))) / 3600;
// const lngOffset = (x - map.width / 2) * (180 / (256 * Math.pow(2, scale))) / 3600;
// resolve({
// latitude: center.lat + latOffset,
// longitude: center.lng + lngOffset
// });
// }
// });
// });
}
// 计算可视区域四个角点
export function calculateVisibleBounds(map) {
map.getScale((res) => {
console.log("00000000000000000000", res)
})
// const topLeft = await pixelToLatLng(map, 0, 0);
// const topRight = await pixelToLatLng(map, width, 0);
// const bottomLeft = await pixelToLatLng(map, 0, height);
// const bottomRight = await pixelToLatLng(map, width, height);
// return {
// minLat: Math.min(topLeft.latitude, bottomLeft.latitude),
// maxLat: Math.max(topRight.latitude, bottomRight.latitude),
// minLng: Math.min(topLeft.longitude, topRight.longitude),
// maxLng: Math.max(bottomLeft.longitude, bottomRight.longitude)
// };
}
// 判断点是否在多边形内(射线法)
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) {
debugger;
const dx = longitude - center.lng; // 经度差
const dy = latitude - center.lat; // 纬度差
const distance = Math.sqrt(dx * dx + dy * dy);
return distance <= radius;
2025-04-23 16:03:22 +08:00
}