Commit be63bdeb by wangzhihui

高德地图替换百度地图

1 parent 2917f8e2
...@@ -99,7 +99,7 @@ ...@@ -99,7 +99,7 @@
</v-full-layout> </v-full-layout>
</template> </template>
<script> <script>
import mapService from "@/utils/mapService.js"; import mapService from "@/utils/mapBService.js";
import "@/utils/mapEvent.js"; import "@/utils/mapEvent.js";
import { STATICIMAGE } from "@/constant"; import { STATICIMAGE } from "@/constant";
import FenceMapControll from "./FenceMapControll"; import FenceMapControll from "./FenceMapControll";
...@@ -145,6 +145,8 @@ export default { ...@@ -145,6 +145,8 @@ export default {
}, },
], ],
}, },
markers: {},
infowins: {},
hideFooter: false, hideFooter: false,
poi: {}, poi: {},
poiMapSearch: "", poiMapSearch: "",
...@@ -158,43 +160,44 @@ export default { ...@@ -158,43 +160,44 @@ export default {
}, },
mounted() { mounted() {
console.log(STATICIMAGE); console.log(STATICIMAGE);
this.initMap(); this.initBMap();
EventBus.$on("uiMap.poi.marker.delete", this.removePoi); EventBus.$on("uiMap.poi.marker.delete", this.removePoi);
}, },
methods: { methods: {
// 地图初始化 async initBMap() {
initMap() { this.mapObj = await mapService.init("map");
let _this = this; this.mapLoading = false;
let plugins = ["AMap.LngLat", "AMap.MassMarks", "AMap.Geocoder"]; window.AMap = this.mapObj;
_this.$mapInit({ plugins: plugins }).then((AMap) => { this.autoComplete = AMap.invoke("Autocomplete", {
_this.mapLoading = false; input: "poiMapSearch",
window.AMap = AMap;
_this.map = mapService.init("map", {
zoom: 15,
showIndoorMap: false,
showLabel: false,
}); });
_this.bindSearch(); AMap.invoke("autocompleteShow");
_this.initMapEvent(); AMap.invoke("autocompleteAddEventListener", "onconfirm", (e) => {
_this.getPoiTrack(); this.poi = e.item.value;
this.$set(this.form, "address", this.poi.address);
this.$refs.addressInput.focus();
}); });
this.initMapEvent();
this.getPoiTrack();
}, },
// 创建地图右键菜单 // 创建地图右键菜单
initMapEvent() { initMapEvent() {
let _this = this; const self = this;
let _map = this.map; const mapInstance = mapService.mapInstance;
var contextMenu = AMap.invoke("ContextMenu");
let _position = ""; let _position = "";
let _contextMenu = new AMap.ContextMenu();
//右键添加Marker标记 contextMenu.addItem(
_contextMenu.addItem("设置POI点", function () { AMap.invoke("MenuItem", "设置POI点", function () {
_contextMenu.close(); self.addPoiMarker(_position);
_this.addPoiMarker(_position); })
}); );
//地图绑定鼠标右击事件——弹出右键菜单
_map.on("rightclick", function (e) { AMap.invoke("mapAddEventListener", "rightclick", function (e) {
_contextMenu.open(_map, e.lnglat); console.log(e)
_position = e.lnglat; AMap.invoke("addContextMenu", contextMenu);
_position = e.latlng;
}); });
}, },
...@@ -203,72 +206,71 @@ export default { ...@@ -203,72 +206,71 @@ export default {
let _this = this; let _this = this;
let _query = _this.$route.query; let _query = _this.$route.query;
let markers = []; let markers = [];
let _icon = {
url: STATICIMAGE.POI_MASS_ICON,
size: new AMap.Size(16, 21),
anchor: new AMap.Pixel(3, 3),
};
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];
if (k.id != _query.id) {
if (k.lnglat) { if (k.lnglat) {
let m = k.lnglat.split(","); let m = k.lnglat.split(",");
k.lng = m[0]; let lnglatXY = mapService.getLngLat(m[0], m[1]);
k.lat = m[1]; markers.push(lnglatXY);
k.lnglat = m; let marker = mapService.createPoiMarker(lnglatXY, AMap);
markers.push(k); AMap.invoke("addOverlay", marker);
} AMap.invoke("markerAddEventListener", "click", (e) => {
let data = { ...e, ...k };
this.markerEvent(data);
});
} }
} }
let mass = new AMap.MassMarks(markers, { AMap.invoke("setViewport", markers);
opacity: 0.8,
zIndex: 10,
cursor: "pointer",
style: _icon,
});
mass.on("click", function (e) {
console.log(e);
_this.markerEvent(e.data);
});
mass.setMap(_this.map);
}, },
// 地图右键添加poi点 // 地图右键添加poi点
addPoiMarker(options, type, isOpen) { addPoiMarker(options, type, isOpen) {
console.log(options,type, isOpen);
let _this = this; let _this = this;
let _map = _this.map; let _map = _this.map;
let _InfoWindow = _this.poiInfoWindow; let _InfoWindow = _this.poiInfoWindow;
if (_this.poiMarker) { if (_this.poiMarker) {
_map.remove(_this.poiMarker); AMap.invoke("removeOverlay", _this.poiMarker);
_InfoWindow && _InfoWindow.close(); _InfoWindow && AMap.invoke("mapCloseInfoWindow", _InfoWindow);
_this.poiMarker = null; _this.poiMarker = null;
} }
let _icon = new AMap.Icon({ let _icon = AMap.invoke(
image: STATICIMAGE.POI_MARKER, "Icon",
}); STATICIMAGE.POI_MARKER,
AMap.invoke("Size", 24, 24)
);
if (options.id) { if (options.id) {
_icon = new AMap.Icon({ _icon = AMap.invoke(
image: STATICIMAGE.POI_MASS_ICON, "Icon",
imageSize: new AMap.Size(16, 21), STATICIMAGE.POI_MASS_ICON,
}); AMap.invoke("Size", 16, 21)
);
} }
let lnglatXY=mapService.getLngLat(options.lng, options.lat);
let _marker = new AMap.Marker({ let _marker=AMap.invoke('Marker', lnglatXY, {
icon: _icon, icon:_icon
position: [options.lng, options.lat], })
extData: options, // let _marker = new AMap.Marker({
}); // icon: _icon,
// position: [options.lng, options.lat],
let _markerInfo = _marker.getExtData(); // extData: options,
//_marker.infoWindow = mapService.createPoiMarkerInfowindow(options); // });
_marker.on("click", function (e) { let _markerInfo=options
_this.markerEvent(_markerInfo); _markerInfo.latLng=mapService.getLngLat(options.lng, options.lat);
AMap.invoke("addOverlay", _marker);
this.markerEvent(_markerInfo);
// AMap.invoke("mapOpenInfoWindow", infoWindow,lnglat)
AMap.invoke("markerAddEventListener", "click", (e) => {
_markerInfo = { ...e, ..._markerInfo };
this.markerEvent(_markerInfo);
}); });
_marker.setMap(_this.map); // let _markerInfo = _marker.getExtData();
// //_marker.infoWindow = mapService.createPoiMarkerInfowindow(options);
// _marker.on("click", function (e) {
// _this.markerEvent(_markerInfo);
// });
// _marker.setMap(_this.map);
// 历史marker存储起来,以id为键值,方便后续删除用 // 历史marker存储起来,以id为键值,方便后续删除用
if (options.id) { if (options.id) {
...@@ -295,27 +297,20 @@ export default { ...@@ -295,27 +297,20 @@ export default {
// marker点击事件 // marker点击事件
markerEvent(e) { markerEvent(e) {
let _this = this; console.log(e)
console.log(e);
let _params = e; let _params = e;
const geocoder = new AMap.Geocoder({ this.setPoiInfo(e);
extensions: "all", let point = e.latLng;
}); AMap.invoke("getLocation", point, (res) => {
_this.setPoiInfo(e); console.log(res)
geocoder.getAddress( let _pois = res.surroundingPois || [];
new AMap.LngLat(e.lng, e.lat), _params.address = res.address;
function (status, result) { _params.near = _pois[0] ? _pois[0].title : "";
console.log(status);
console.log(result);
let _regeocode = result.regeocode || {};
let _pois = _regeocode.pois || [];
_params.address = _regeocode.formattedAddress || "";
_params.near = _pois[0] ? _pois[0].name : "";
let infoWindow = mapService.createPoiMarkerInfowindow(_params); let infoWindow = mapService.createPoiMarkerInfowindow(_params);
_this.poiInfoWindow = infoWindow; this.poiInfoWindow = infoWindow;
infoWindow.open(_this.map); AMap.invoke("mapOpenInfoWindow", infoWindow, point);
} AMap.invoke("centerAndZoom",point,14)
); });
}, },
// 设置poi信息 // 设置poi信息
...@@ -439,8 +434,8 @@ export default { ...@@ -439,8 +434,8 @@ export default {
let _InfoWindow = this.poiInfoWindow; let _InfoWindow = this.poiInfoWindow;
// id不存在,临时的poi点,删除marker就行了 // id不存在,临时的poi点,删除marker就行了
if (!row.id) { if (!row.id) {
_map.remove(_this.poiMarker); AMap.invoke('removeOverlay', _this.poiMarker)
_InfoWindow && _InfoWindow.close(); _InfoWindow && AMap.invoke('mapCloseInfoWindow', _InfoWindow);
_this.form = {}; _this.form = {};
setTimeout(function () { setTimeout(function () {
_this.$refs.poiForm.clearValidate(); _this.$refs.poiForm.clearValidate();
...@@ -451,8 +446,8 @@ export default { ...@@ -451,8 +446,8 @@ export default {
DELETE_POI_INFO(_params).then((res) => { DELETE_POI_INFO(_params).then((res) => {
if (res.code == 200) { if (res.code == 200) {
let _marker = _this.markerList[row.id]; let _marker = _this.markerList[row.id];
_map.remove(_marker); AMap.invoke('removeOverlay', _marker)
_InfoWindow && _InfoWindow.close(); _InfoWindow && AMap.invoke('mapCloseInfoWindow', _InfoWindow);
showToast({ showToast({
message: "删除成功", message: "删除成功",
type: "success", type: "success",
...@@ -473,13 +468,12 @@ export default { ...@@ -473,13 +468,12 @@ export default {
if (res.code == 200) { if (res.code == 200) {
let _list = res.data || []; let _list = res.data || [];
_this.addMassMarkers(_list); _this.addMassMarkers(_list);
// 编辑页面 // 编辑页面
if (_query.id) { if (_query.id) {
_this.getInfo(); _this.getInfo();
} }
_this.trackList = res.data || []; _this.trackList = res.data || [];
_this.showRoadTitle(); // _this.showRoadTitle();
} }
}); });
}, },
...@@ -526,7 +520,8 @@ export default { ...@@ -526,7 +520,8 @@ export default {
if (_map) { if (_map) {
console.log(lng, lat); console.log(lng, lat);
setTimeout(function () { setTimeout(function () {
_map.setCenter([lng, lat]); let lnglatXY = mapService.getLngLat(lng, lat);
AMap.invoke('panTo',lnglatXY)
}, 1000); }, 1000);
} }
}, },
......
...@@ -343,10 +343,10 @@ export default { ...@@ -343,10 +343,10 @@ export default {
}, },
methods: { methods: {
//初始化百度地图 //初始化百度地图
async initBMap(){ async initBMap() {
this.mapObj = await mapService.init("map"); this.mapObj = await mapService.init("map");
window.AMap = this.mapObj; window.AMap = this.mapObj;
this.getDevinceInfo() this.getDevinceInfo();
}, },
// 获取设备信息 // 获取设备信息
...@@ -476,10 +476,9 @@ export default { ...@@ -476,10 +476,9 @@ export default {
// 监听圆形围栏直径变化 // 监听圆形围栏直径变化
onFenceRadiusChange(e) { onFenceRadiusChange(e) {
if(e){ if (e) {
this.$set(this.form, "radius", e); this.$set(this.form, "radius", e);
} }
}, },
// 监听围栏类型变化 // 监听围栏类型变化
...@@ -490,9 +489,11 @@ export default { ...@@ -490,9 +489,11 @@ export default {
// 1:圆形围栏 4:行政区域 3:多边形 // 1:圆形围栏 4:行政区域 3:多边形
switch (e) { switch (e) {
case 4: case 4:
// AMap.invoke("removeContextMenu");
_this.drawDistrict(areaText); _this.drawDistrict(areaText);
break; break;
case 3: case 3:
// AMap.invoke("removeContextMenu");
_this.drawPolygon(); _this.drawPolygon();
break; break;
default: default:
......
...@@ -40,8 +40,8 @@ const historyLocation = { ...@@ -40,8 +40,8 @@ const historyLocation = {
const MAP = mapService.map; const MAP = mapService.map;
const lnglat = this.markers[idx].getPosition(); const lnglat = this.markers[idx].getPosition();
// this.infowins[idx].open(MAP, lnglat); // this.infowins[idx].open(MAP, lnglat);
MAP.invoke("mapOpenInfoWindow", this.infowins[idx],lnglat) AMap.invoke("mapOpenInfoWindow", this.infowins[idx],lnglat)
MAP.invoke('centerAndZoom',lnglat,10) AMap.invoke('centerAndZoom',lnglat,10)
} }
}); });
...@@ -49,9 +49,11 @@ const historyLocation = { ...@@ -49,9 +49,11 @@ const historyLocation = {
createClusterer(){ createClusterer(){
let map=mapService.mapInstance; let map=mapService.mapInstance;
var cluster, markers = []; var cluster, markers = [];
for(let idx in this.markers){ for(let idx in this.markers){
markers.push(this.markers[idx]); markers.push(this.markers[idx]);
} }
console.log(markers)
this.cluster = AMap.invoke('Cluster', markers, data => { this.cluster = AMap.invoke('Cluster', markers, data => {
return { return {
point: [data.latLng.lng, data.latLng.lat], point: [data.latLng.lng, data.latLng.lat],
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: 'wangzhihui' '1733904062@qq.com' * @Author: 'wangzhihui' '1733904062@qq.com'
* @Date: 2024-12-06 14:10:32 * @Date: 2024-12-06 14:10:32
* @LastEditors: 'wangzhihui' '1733904062@qq.com' * @LastEditors: 'wangzhihui' '1733904062@qq.com'
* @LastEditTime: 2024-12-20 11:23:44 * @LastEditTime: 2024-12-21 15:52:17
* @FilePath: \oEshield\src\utils\mapBService.js * @FilePath: \oEshield\src\utils\mapBService.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
...@@ -214,6 +214,16 @@ MapService.prototype = { ...@@ -214,6 +214,16 @@ MapService.prototype = {
marker.setAnimation(BMAP_ANIMATION_DROP) marker.setAnimation(BMAP_ANIMATION_DROP)
return marker; return marker;
}, },
createPoiMarker(lnglatXY, map) {
let that=this,
url=require('images/poi-marker-default.png'),
size=that.map.invoke('Size', 24, 34)
var marker=that.map.invoke('Marker', lnglatXY, {
icon:that.map.invoke('Icon', url,size)
})
marker.setAnimation(BMAP_ANIMATION_DROP)
return marker;
},
createStartMarker: function(lnglatXY, map) { createStartMarker: function(lnglatXY, map) {
let that=this, let that=this,
url=require('images/icon_start.png'), url=require('images/icon_start.png'),
...@@ -945,6 +955,8 @@ MapService.prototype = { ...@@ -945,6 +955,8 @@ MapService.prototype = {
// 创建poi信息弹框 // 创建poi信息弹框
createPoiMarkerInfowindow(info) { createPoiMarkerInfowindow(info) {
console.log(info)
let self=this;
let infoDiv = document.createElement('div'); let infoDiv = document.createElement('div');
let _div = `<div class="map_poi_infowindow_popup"><h3 class="map_poi_infowindow_title">${info.address || '暂无地址'}</h3> let _div = `<div class="map_poi_infowindow_popup"><h3 class="map_poi_infowindow_title">${info.address || '暂无地址'}</h3>
<div class="map_poi_infowindow_content"> <div class="map_poi_infowindow_content">
......
...@@ -66,6 +66,7 @@ const replay = { ...@@ -66,6 +66,7 @@ const replay = {
EventBus.$emit('uiMap.replay.initDone') EventBus.$emit('uiMap.replay.initDone')
AMap.invoke('lushuAddEventListener','move', (e) =>{ AMap.invoke('lushuAddEventListener','move', (e) =>{
this.idx = e.target.i; this.idx = e.target.i;
console.log(this.idx)
let lnglat = replayData[this.idx] let lnglat = replayData[this.idx]
let item=replay.data[this.idx] let item=replay.data[this.idx]
AMap.invoke('lushuSetContent',replay.getNavgInfoContent(item)) //设置信息窗口文案 AMap.invoke('lushuSetContent',replay.getNavgInfoContent(item)) //设置信息窗口文案
...@@ -133,6 +134,7 @@ const replay = { ...@@ -133,6 +134,7 @@ const replay = {
//轨迹暂停 //轨迹暂停
EventBus.$on('uiMap.replay.pause', (ifShowAddr)=>{ EventBus.$on('uiMap.replay.pause', (ifShowAddr)=>{
console.log(this.idx)
if(replay.navg){ if(replay.navg){
AMap.invoke('lushuPause') AMap.invoke('lushuPause')
if(ifShowAddr){ if(ifShowAddr){
...@@ -172,10 +174,12 @@ const replay = { ...@@ -172,10 +174,12 @@ const replay = {
EventBus.$on('uiMap.replay.drawLocation', (data)=>{ EventBus.$on('uiMap.replay.drawLocation', (data)=>{
const MAP = mapService.map; const MAP = mapService.map;
for(let idx in this.markers){ for(let idx in this.markers){
MAP.remove(this.markers[idx]) // MAP.remove(this.markers[idx])
AMap.invoke('removeOverlay', this.markers[idx])
} }
for(let idx in this.infowins){ for(let idx in this.infowins){
MAP.remove(this.infowins[idx]) // MAP.remove(this.infowins[idx])
AMap.invoke('removeOverlay', this.infowins[idx])
} }
this.markers = {}; this.markers = {};
this.infowins = {}; this.infowins = {};
...@@ -193,26 +197,45 @@ const replay = { ...@@ -193,26 +197,45 @@ const replay = {
if(this.markers[idx]){ if(this.markers[idx]){
const MAP = mapService.map; const MAP = mapService.map;
const lnglat = this.markers[idx].getPosition(); const lnglat = this.markers[idx].getPosition();
this.infowins[idx].open(MAP, lnglat); AMap.invoke('mapOpenInfoWindow', this.infowins[idx], lnglat)
// this.infowins[idx].open(MAP, lnglat);
EventBus.$emit('uiMap.replay.pause'); EventBus.$emit('uiMap.replay.pause');
EventBus.$emit('uiMap.replay.replayIng', false); EventBus.$emit('uiMap.replay.replayIng', false);
MAP.setZoomAndCenter(MAP.getZoom(), lnglat); // MAP.setZoomAndCenter(MAP.getZoom(), lnglat);
AMap.invoke('panTo', lnglat)
} }
}); });
}, },
drawLocation(data){ drawLocation(data) {
const MAP = mapService.map; // console.log(data)
EventBus.$emit('uiMap.historyLocation.transferSuccess', data); const { MAP, mapapi } = mapService
data.forEach((item)=>{ EventBus.$emit('uiMap.historyLocation.transferSuccess', data)
let marker = mapService.createPointMarker([item.elng, item.elat], MAP); data.forEach((item) => {
let infoWindow = mapService.bindLocationInfoWindow(marker, MAP, item); const marker = mapService.createPointMarker(mapService.getLngLat(item.elng, item.elat), MAP)
this.markers[item.idx] = marker; AMap.invoke('addOverlay', marker)
this.infowins[item.idx] = infoWindow; const infoWindow = mapService.bindLocationInfoWindow(marker, MAP, item)
}); this.markers[item.idx] = marker
this.infowins[item.idx] = infoWindow
//mapService.setFitView(MAP); })
// console.log(this.markers, 'markers')
const points = Object.values(this.markers).map(item => item.latLng)
// console.log(points)
AMap.invoke('setViewport', points)
// mapService.setFitView(MAP);
}, },
// drawLocation(data){
// EventBus.$emit('uiMap.historyLocation.transferSuccess', data);
// data.forEach((item)=>{
// let lnglatXY=mapService.getLngLat(item.elng, item.elat);
// let marker = mapService.createPointMarker(lnglatXY, MAP);
// let infoWindow = mapService.bindLocationInfoWindow(marker, MAP, item);
// this.markers[item.idx] = marker;
// this.infowins[item.idx] = infoWindow;
// });
// //mapService.setFitView(MAP);
// },
speedEvent(idx, dis) { speedEvent(idx, dis) {
console.log(idx, dis) console.log(idx, dis)
let timeArr = [[40, 30, 16], [30, 20, 12], [20, 12, 8], [5, 3, 2]] // 慢 正常 快 很快 let timeArr = [[40, 30, 16], [30, 20, 12], [20, 12, 8], [5, 3, 2]] // 慢 正常 快 很快
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!