antV L7地图 自定义Popup弹窗 setHTML

一、创建地图

        先将地图渲染出来,效果和代码如下

<div class="container">
    <div class="container-top">
      <el-button type="warning" @click="toHome()">返回首屏</el-button>
    </div>
    <div class="container-content">
       <div id="map"></div>
    </div>
</div>

import { ref, onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { PointLayer, PolygonLayer, Scene, LineLayer, Popup } from '@antv/l7'    //引入各种需要用到的图层
import { load } from "@amap/amap-jsapi-loader";    //如果不使用高德地图实例,这一步可省略
import { GaodeMap } from '@antv/l7-maps'
import sichuanJSON from '@/assets/json/sichuan.json'     //引入四川json

let router = useRouter()
//地图实例
let scene: any = ref(null)
//模拟点位数据
let nodesList = reactive([
    {
        id: 1,
        lat: 30.12207207,
        lng: 103.82112953,
        name: '一号点位',
        creatTime: '2023-11-13 16:43',
        Area: '四川省成都市',
        detailAddress: '锦江区锦江街道1号'
    }
])
//初始化地图
async function initMap() {
    let AMap = await load({
        key: "自己注册的高德地图key",
        version: "2.0",
        plugins: [
            "AMap.DistrictSearch",
            "AMap.DistrictLayer",
            "AMap.TileLayer",
            "AMap.Polygon",
            "AMap.GeoJSON",
            "AMap.Weather",
        ],
    })
    let gaodeMap = new AMap.Map('map', {
        center: [103.82112953, 30.12207207],
        zoom: 7,
        pitch: "30",
        viewMode: "2D",
        layers: [
            new AMap.TileLayer.Satellite()      // 卫星图层
        ]
    })
    scene.value = new Scene({
        id: 'map',
        logoVisible: false,
        map: new GaodeMap({
            mapInstance: gaodeMap,  //地图实例使用高德地图(如使用官方地图可查看官网地图的创建)
        })
    })
    
}
//路由返回上一页
function toHome() {
    router.go(-1)
}
//执行初始化
onMounted(() => {
    initMap()
})

.container {
    width: 100%;
    height: 100%;

    &-top {
        height: 5%;
    }

    &-content {
        height: 95%;

        #map {
            width: 100%;
            height: 100%;
        }
        
    }
}

二、添加各个图层

根据业务逻辑添加自己需要的图层(本demo里,为了看起来完整,分别添加了面图层、线图层、点位图层 各1个)

async function initMap() {
    ......
    scene.value.on('loaded', () => {
        //添加图层
        addLayer()
    })
}

function addLayer() {
    //添加四川地图面图层
    let sichuanLayer = new PolygonLayer()
        .source(sichuanJSON)
        .color('rgba(120, 142, 144, 0.8)')
    //添加四川各市的分界线
    let sichuanLineLayer = new LineLayer()
        .source(sichuanJSON)
        .color('#fff')
    //添加点位图层
    let nodesLayer = new PointLayer()
        .source(nodesList, {
            parser: {
                type: 'json',
                x: 'lng',
                y: 'lat'
            }
        })
        .shape('cylinder')
        .size(16)
        .color('red')
  
    scene.value.addLayer(sichuanLayer)
    scene.value.addLayer(sichuanLineLayer)
    scene.value.addLayer(nodesLayer)
}

三、给点图层添加点击事件(展示自定义弹窗)

在地图上先新增自定义弹窗的dom,然后将该dom放进弹窗里。在dom中设置宽高背景色,但是会有Popup的默认内边距,如图

<div class="container-content">
   <div id="map"></div>
必须要用v-show,不然获取不到dom元素
   <div id="custom" v-show="flag">
        我是自定义的Popup弹窗
   </div>
</div>

let flag = ref(false)    //控制弹窗显影

function addLayer() {
    .......
     nodesLayer.on('click', (val: any) => {
        
        let customBox: any = document.getElementById('custom')    //获取自定义的dom元素
        const { lat, lng } = val.feature    //获取经纬度
        const popup = new Popup({
            // 初始锚点经纬度
            lngLat: {
                lat,
                lng
            },
        });
        flag.value = true
        popup.setHTML(customBox)
        scene.value.addPopup(popup);
}


#custom{
            background-color: red;
            width: 200px;
            height: 200px;
        }

四、清除Popup的默认内外边距

<style>
    ......
    #custom{
            width: 200px;
            height: 200px;
        }

    :deep(.l7-popup) {
            min-width: 280px !important;
            border-radius: 5px;

            .l7-popup-tip {
                border-top-color: #122c1f;
            }

            .l7-popup-content {
                color: #fff;
                background: #122c1f !important;
                border: 1px solid #1f977d !important;

                .l7-popup-close-button {
                    fill: #fff;
                }

                .l7-layer-popup__value {
                    font-size: 16px;
                    color: aqua;
                }
            }
        }
</style>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
AntV L7一个基于WebGL的地理信息可视化开发库,可以在Vue项目使用。要在Vue项目使用AntV L7,首先需要在HTML文件引入L7的脚本文件。可以使用CDN方式引入,例如: ```html <head> <!-- 引入最新版的L7 --> <script src='https://unpkg.com/@antv/l7'></script> <!-- 指定版本号引入L7 --> <script src='https://unpkg.com/@antv/l7@2.0.11'></script> </head> ``` 接下来,在Vue组件安装AntV L7以及相关的组件引用。可以使用npm或yarn安装AntV L7,然后在Vue组件引入所需的组件。例如,在data创建一个变量来接收地图实例和组件: ```javascript import { Scene } from '@antv/l7'; export default { data() { return { scene: null, // 其他变量 } }, // 其他生命周期钩子函数 } ``` 在mounted钩子函数生成地图实例,并加载数据生成地图。可以根据需求进行异步操作数据。例如: ```javascript mounted() { this.scene = new Scene({ // 地图配置 }); // 加载数据生成地图 this.loadDataAndCreateMap(); }, methods: { async loadDataAndCreateMap() { const data = await this.loadData(); // 异步加载数据 // 根据数据生成地图 // this.scene.addLayer(...) // 其他操作 }, // 其他方法 } ``` 最后,可以在Vue组件渲染地图的DOM元素。例如,在HTML模板添加一个div元素来渲染地图: ```html <template> <div id="mapContainer"></div> </template> ``` 这样就可以在Vue项目使用AntV L7进行地图可视化开发了。请注意,具体的代码实现可能会根据项目需求有所不同,以上只是一个简单的示例。 #### 引用[.reference_title] - *1* [【Antv/Vue3】vue项目使用antv/L7制作地图](https://blog.csdn.net/weixin_52378152/article/details/125429250)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [在vue项目使用AntV L7地图下钻,异步调用不重复生成](https://blog.csdn.net/Grupass/article/details/127103298)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [antvl7绘制地图以及vue项目使用多线程worker](https://blog.csdn.net/Mr__proto__/article/details/128975829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值