我使用vue,将GIS地图存入vuex中,因为需要做大屏,地图好几处需要,所以存入vuex中,在组件中使用GIS的方法之前要监听地图是否加载完成,后执行操作地图方法,否则会报错
效果图如下图:
1、在组件中使用GIS,在地图上添加点位
import { loadModules } from 'esri-loader'
loadModules([
'esri/Graphic',
'esri/layers/GraphicsLayer'
], {
url: 'https://js.arcgis.com/4.19/',
css: 'https://js.arcgis.com/4.19/esri/themes/light/main.css'
}).then(([Graphic, GraphicsLayer])=>{
let graphicsLayerGather = [] // 需要在地图上描绘的点位集合
// val从后端取来的数组格式的数据
val.forEach(point => {
let symbol = {
type:'picture-marker',
width: '45px',
height: '35px',
url:'图标地址'
}
let Ghc = new Graphic({
attributes: JSON.stringify(point),
geometry: {
type: 'point',
longitude: point.x,//经度
latitude: point.y //纬度
},
symbol: symbol,
id: 'dyname-point',
popupTemplate: {
outFields: ['*'],
content: this.getClickInfo //点击图标点获取到的点位数据
}
})
graphicsLayerGather.push(Ghc)
})
let graphicsLayer = new GraphicsLayer({
graphics: graphicsLayerGather,
id: 'graphicsLayer',
elevationInfo: {
mode: 'relative-to-scene',
featureExpressionInfo: {
expression: '50'
},
unit: 'meters'
}
})
this.mapView.map.add(graphicsLayer) //将图标添加至地图上
// 当全部点位加载完成之后,执行其他操作
graphicsLayer.when(() => {})
})
设置arcgis for javscript4.19自身弹窗是否可见
mapView.popup.visible = false
2、点击点位图标进行高亮选中状态,进行方法操作:
思路:
1、首先加载所有点位至地图上;
2、然后根据ID拿到graphics 项,使用唯一标识找到相对应的图标对象进行替换;
getClickInfo(){
let pointInf = value.graphic.attributes
let center = [Number(pointInf.x), Number(pointInf.y)]
let zoom = 16
// 获取 graphics 项 graphicsLayer 是上面加载点位的ID
let currentPoint = this.mapView.map.findLayerById('graphicsLayer').graphics.items
// 过滤出指定 pointId 的项 此处需要修改为你的字段的名称
let pointIndex = currentPoint.findIndex(item => {
return JSON.parse(item.attributes).pointId === pointInf.pointId
})
//判断是否找到,进行设置symbol的点位图标大小
if (pointIndex !== -1) {
currentPoint[pointIndex].symbol = {
type: 'picture-marker',
width: '85px',
height: '45px',
url:'图标地址'
}
// 点位进行定位方法操作功能
this.goToPoint(center, zoom)
}
}
3、点位进行定位放大操作功能
goToPoint(center, zoom){
mapView.goTo(
{
center: center,
zoom: zoom
},
{
speedFactor: 0.8,
easing: 'linear'
},
{
duration: 1000,
easing: 'in-out-expo'
}
).catch((error) => {
if (error.name !== 'AbortError') {
console.error(error)
}
})
}