Commit 940401da by liimin

refactor(zoneCar): 优化地图标记添加逻辑并提取公共方法

将标记创建逻辑提取为独立方法_createMarker,优化addMarkers方法结构
统一处理坐标转换逻辑,增加isTranslate参数控制是否进行坐标转换
修复地址字段空值处理,使用统一格式的空字符串
1 parent e628e8f3
...@@ -233,7 +233,7 @@ export default { ...@@ -233,7 +233,7 @@ export default {
this.mapLoading = false; this.mapLoading = false;
window.AMap = this.mapObj; window.AMap = this.mapObj;
AMap.invoke("LocalCity", (res) => { AMap.invoke("LocalCity", (res) => {
AMap.invoke('panTo',res.center) AMap.invoke("panTo", res.center);
}); });
this.autoComplete = AMap.invoke("Autocomplete", { this.autoComplete = AMap.invoke("Autocomplete", {
input: "tipInput", input: "tipInput",
...@@ -241,7 +241,7 @@ export default { ...@@ -241,7 +241,7 @@ export default {
AMap.invoke("autocompleteShow"); AMap.invoke("autocompleteShow");
AMap.invoke("autocompleteAddEventListener", "onconfirm", (e) => { AMap.invoke("autocompleteAddEventListener", "onconfirm", (e) => {
this.poi = e.item.value; this.poi = e.item.value;
this.$set(this.form, "address", this.poi.address||''); this.$set(this.form, "address", this.poi.address || "");
this.$refs.addressInput.focus(); this.$refs.addressInput.focus();
}); });
...@@ -557,7 +557,7 @@ export default { ...@@ -557,7 +557,7 @@ export default {
let m = o.gpsLocation || {}; let m = o.gpsLocation || {};
o.lng = m.blng; o.lng = m.blng;
o.lat = m.blat; o.lat = m.blat;
o.address = m.address || ''; o.address = m.address || "";
o.type = 2; o.type = 2;
o.time = m.dateTime; o.time = m.dateTime;
o.standNo = item.standNo || item.vin; o.standNo = item.standNo || item.vin;
...@@ -565,25 +565,37 @@ export default { ...@@ -565,25 +565,37 @@ export default {
arr.push(o); arr.push(o);
_this.location = o; _this.location = o;
}); });
_this.addMarkers(arr); _this.addMarkers(arr, false);
}); });
}); });
}, },
// 将坐标转换封装为返回 Promise 的函数 // 将坐标转换封装为返回 Promise 的函数
convertCoordinate(item) { convertCoordinate(item) {
item = item.split(",") item = item.split(",");
return new Promise((resolve) => { return new Promise((resolve) => {
AMap.invoke('Convertor', [AMap.invoke('Point', item[0], item[1])], 5, 3, res => { AMap.invoke(
"Convertor",
[AMap.invoke("Point", item[0], item[1])],
5,
3,
(res) => {
const convertedPoint = res.points.length ? res.points[0] : item; const convertedPoint = res.points.length ? res.points[0] : item;
resolve(convertedPoint); resolve(convertedPoint);
}); }
);
}); });
}, },
AMapPoint2BMapPoint(AMapPoint,callback) { AMapPoint2BMapPoint(AMapPoint, callback) {
AMap.invoke('Convertor', [AMap.invoke('Point', AMapPoint.lng, AMapPoint.lat)], 3, 5, res => { AMap.invoke(
"Convertor",
[AMap.invoke("Point", AMapPoint.lng, AMapPoint.lat)],
3,
5,
(res) => {
const convertedPoint = res.points.length ? res.points[0] : item; const convertedPoint = res.points.length ? res.points[0] : item;
callback(convertedPoint); callback(convertedPoint);
}); }
);
}, },
// 区域查车 // 区域查车
async getAreaLocation() { async getAreaLocation() {
...@@ -610,14 +622,10 @@ export default { ...@@ -610,14 +622,10 @@ export default {
_this.loading = true; _this.loading = true;
delete params.paths; delete params.paths;
let left = await this.convertCoordinate(params.leftLngLat) let left = await this.convertCoordinate(params.leftLngLat);
let right = await this.convertCoordinate(params.rightLngLat) let right = await this.convertCoordinate(params.rightLngLat);
params.leftLngLat = `${left.lng},${ params.leftLngLat = `${left.lng},${left.lat}`;
left.lat params.rightLngLat = `${right.lng},${right.lat}`;
}`;
params.rightLngLat = `${right.lng},${
right.lat
}`;
let res = await GER_REGIONAL_SEARCH(params); let res = await GER_REGIONAL_SEARCH(params);
console.log(res, "res"); console.log(res, "res");
_this.loading = false; _this.loading = false;
...@@ -630,7 +638,7 @@ export default { ...@@ -630,7 +638,7 @@ export default {
let list = res.data || []; let list = res.data || [];
_this.deviceList = list; _this.deviceList = list;
_this.hideFooter = false; _this.hideFooter = false;
_this.addMarkers(list); _this.addMarkers(list, true);
}, },
// 在地图上画行政区域 // 在地图上画行政区域
...@@ -638,25 +646,30 @@ export default { ...@@ -638,25 +646,30 @@ export default {
EventBus.$emit("uiMap.fence.drawDistrict", areaCode); EventBus.$emit("uiMap.fence.drawDistrict", areaCode);
}, },
// 添加海量点 (MassMarks方式) // 添加海量点 (MassMarks方式)
addMarkers(arr) { addMarkers(arr, isTranslate) {
let _this = this; let _this = this;
let markers = []; let markers = [];
console.log(arr);
for (let i = 0, len = arr.length; i < len; i++) { for (let i = 0, len = arr.length; i < len; i++) {
let k = arr[i]; let k = arr[i];
console.log(k);
let lnglat = mapService.getLngLat(k.lng, k.lat); let lnglat = mapService.getLngLat(k.lng, k.lat);
this.AMapPoint2BMapPoint(lnglat,(carPoint)=>{ //写死了 高德地图转百度地图 TODO: 动态判断 if (isTranslate) {
let marker = mapService.createCarMarker(carPoint); this.AMapPoint2BMapPoint(lnglat, (translateLngLat) => {//写死了 高德地图转百度地图 TODO: 动态判断
let marker = _this._createMarker(translateLngLat);
markers.push(marker); markers.push(marker);
AMap.invoke("addOverlay", marker); // let marker = mapService.createCarMarker(carPoint);
AMap.invoke("panTo", lnglat); // markers.push(marker);
AMap.invoke("markerAddEventListener", "click", (e) => { // AMap.invoke("addOverlay", marker);
console.log(e); // AMap.invoke("panTo", lnglat);
let infoWindow = mapService.createAraeInfowindow(k); // AMap.invoke("markerAddEventListener", "click", (e) => {
AMap.invoke("mapOpenInfoWindow", infoWindow, e.target.getPosition()); // console.log(e);
// let infoWindow = mapService.createAraeInfowindow(k);
// AMap.invoke("mapOpenInfoWindow", infoWindow, e.target.getPosition());
// });
}); });
}) } else {
let marker = _this._createMarker(lnglat);
markers.push(marker);
}
} }
this.drawDone = true; this.drawDone = true;
// if (arr && arr.length < 10) { // if (arr && arr.length < 10) {
...@@ -665,12 +678,23 @@ export default { ...@@ -665,12 +678,23 @@ export default {
this.mapMass = markers; this.mapMass = markers;
}, },
_createMarker(lngLat) {
let marker = mapService.createCarMarker(lngLat);
AMap.invoke("addOverlay", marker);
AMap.invoke("panTo", lngLat);
AMap.invoke("markerAddEventListener", "click", (e) => {
let infoWindow = mapService.createAraeInfowindow(lngLat);
AMap.invoke("mapOpenInfoWindow", infoWindow, e.target.getPosition());
});
return marker;
},
// 全屏状态下,根据参数初始化页面 // 全屏状态下,根据参数初始化页面
initPage(query) { initPage(query) {
console.log(query); console.log(query);
this.menuId = query.menuId; this.menuId = query.menuId;
if (query.address) { if (query.address) {
this.$set(this.form, "address", query.address||''); this.$set(this.form, "address", query.address || "");
} }
if (query.sn) { if (query.sn) {
this.$set(this.form, "sn", query.sn); this.$set(this.form, "sn", query.sn);
...@@ -692,7 +716,7 @@ export default { ...@@ -692,7 +716,7 @@ export default {
this.drawRectangle(query); this.drawRectangle(query);
this.getAreaLocation(); this.getAreaLocation();
this.autoComplete.setInputValue(this.form.address||''); this.autoComplete.setInputValue(this.form.address || "");
}, },
// 绘画矩形 // 绘画矩形
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!