Commit be63bdeb by wangzhihui

高德地图替换百度地图

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