当前位置: 首页 > news >正文

vue3封装leaflet并使用高德底图

该组件配置了高德和腾讯地图的底图。具有新建图层和打点(使用leaflet中的方法),打点分为单个和多个。经纬度和详细地址的互相转换由高德api完成。

<template><div class="container"><!-- 搜索框 --><div class="module-top"><el-input  v-model="modelValue" placeholder="" style="width: 100%;" @input="onInput"/> </div><!-- 工具栏 --><div class="module-tools"><div class='tools-box'><button @click="onClickMark(1)">打点-单个</button><button @click="onClickMark(2)">打点-多个</button><button @click="onClearLayer">清空</button></div></div><div ref="mapLeafletRef" class="map"></div></div><!-- <button @click="onClearLayer">btn</button> -->
</template>
<script setup>
import L from 'leaflet';
import AMapLoader from '@amap/amap-jsapi-loader';
import { onMounted, onUnmounted, ref, getCurrentInstance, watch } from 'vue';
window._AMapSecurityConfig = {  // 高德安全密钥securityJsCode: '申请的高德安全密钥',
};
const props = defineProps({mapType: {  // 地图底图,高德、腾讯type: String,default: () => {return 'gaode';}}
});
watch(() => props, (newV, oldV) => {console.log(newV, 'new', oldV, 'old');
}, {immediate: true
});
const emits = defineEmits(['doRefreshMap']);
const mapObj = ref(); // leaflet对象
const mapLeafletRef = ref(null); //
const mapGaode = ref(null); // 高德
const modelValue = ref('');
const geocoder = ref(null); // 经纬度解析obj
const layerGroup = ref(null);
const autocomplete = ref(null);
const gaodeMapOption = ref({url: 'http://webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',option: {subdomains: '1234'}
});
const tencentMapOption = ref({url: 'http://rt{s}.map.gtimg.com/realtimerender?z={z}&x={x}&y={y}&type=vector&style=0',option: {subdomains: '0123',tms: true}
});
// 初始化地图:leaflet
const initMap = () => {let mapOption = {};if (props.mapType === 'gaode') {mapOption = gaodeMapOption.value;} else if (props.mapType === 'tengxun') {mapOption = tencentMapOption.value;}const map = L.map(mapLeafletRef.value, {center: [39.90887, 116.397435],zoom: 11,minZoom: 6,maxZoom: 20,zoomControl: true,  //是否启用地图缩放控件attributionControl: false,  是否启用地图属性控件});L.tileLayer(  // 给leaflet添加瓦片底图mapOption.url, mapOption.option).addTo(map);// 绑定leaflet的click点击事件map.on('click', (evt) => {mapClick([evt.latlng.lat, evt.latlng.lng]);});// 创建图层并添加至地图layerGroup.value = L.layerGroup();layerGroup.value.addTo(map);return map;
};
const initMapGaode = () => {AMapLoader.load({key: '申请的高德key', // 申请好的Web端开发者Key,首次调用 load 时必填plugins: ['AMap.Scale', 'AMap.Autocomplete', 'AMap.PlaceSearch'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等AMapUI: {version: '1.1',},}).then((AMap) => {mapGaode.value = AMap;geocoder.value = new AMap.Geocoder({  // 经纬度解析city: '010', // 城市设为北京,默认:“全国”radius: 1000, // 范围,默认:500});const autoOptions = {city: '全国'};autocomplete.value = new AMap.Autocomplete(autoOptions);}).catch((e) => {console.log(e, 'catch');});
};
const marker = ref([]);
const mapClick = (e) => { // leaflet的点击方法// onClearLayer();marker.value = L.marker(e);layerGroup.value.addLayer(marker.value);regeoCode([e[1], e[0]]);
};
const regeoCode = (e) => {  // 经纬度转换成详细地址geocoder.value.getAddress(e, (status, result) => {if (status === 'complete' && result.regeocode) {let address = result.regeocode.formattedAddress;console.log(address, '---address');emits('doRefreshMap', address);} else {console.log('根据经纬度查询地址失败');}});
};
const onClearLayer = () => {  // 清除全部图层上的内容layerGroup.value.clearLayers();
};
const onInput = (e) => {  //input监听输入//console.log(e, '--e');autocomplete.value.search(e, (status, result) => {// 搜索成功时,result即是对应的匹配数据console.log(result, '---result');});
};const removeMap = () => {if (mapObj.value) {mapObj.value.remove();}
};
const show = (e) => {console.log('111');
};
const markNumSingle = ref(true);
const onClickMark = (e) => {if (e === 1) {markNumSingle.value = true;} else {markNumSingle.value = false;}
};
defineExpose({onClearLayer
});
onMounted(() => {mapObj.value = initMap();initMapGaode();
});// 在组件卸载时删除地图
onUnmounted(() => {removeMap();
});
</script><style scoped lang="scss">
.map {height: 100%;width: 100%;z-index: 0;
}
.map2 {height: 40vh;z-index: 0;
}
.container {height: 50vh;position: relative;.module-top{position: absolute;
width: 200px;
height: 50px;
top: 20px;
left: 80px;
z-index: 10;}.module-tools {width:500px;height:50px;position: absolute;bottom:0;left:30%;z-index: 10;background:pink;.tools-box {display:flex;align-items:center;justify-content:center;}}
}</style>

使用leaflet新建一底图的简写方式

let map = L.map('map').setView([39.90887, 116.397435], 17);

复杂icon标点

// latlngs是标点坐标,格式同初始化地图时的中心坐标
let marker = L.marker(latlngs, {icon: L.divIcon({className: "dIcon", //标点icon的容器类名html:`标点icon的html字段内容`, //可以添加标点名字之类的自定义内容// iconSize: [60, 24] //标点icon的容器大小,也可以直接用className类名写css样式})
}).addTo(map); //添加到地图中

相关文章:

  • 学习次模函数-第1章 引言
  • 蓝桥杯算法基础(28)11道关于字符串的小题
  • gateway网关指定路由响应超时时间
  • C#宿舍信息管理系统
  • 基于深度学习的面部情绪识别算法仿真与分析
  • 【LAMMPS学习】三、构建LAMMPS(8)构建 LAMMPS 文档
  • 《python编程快速上手——让繁琐工作自动化》实践项目——逗号代码
  • 微信小程序多图列表页面性能问题为什么会出现?如何解决?
  • Linux环境JMeter脚本性能测试、easyNmon生成监控报告
  • Spring Boot 实现定时任务动态管理
  • web渗透测试漏洞流程:红队攻防流程详细大纲
  • 文献学习-22-Surgical-VQLA:具有门控视觉语言嵌入的转换器,用于机器人手术中的视觉问题本地化回答
  • 【MySQL】3.1MySQL索引的介绍
  • 腾讯云COS - 前端上传文件到 COS 跨域问题
  • 【Lazy ORM 框架学习】
  • [NodeJS] 关于Buffer
  • Docker入门(二) - Dockerfile
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • Markdown 语法简单说明
  • MySQL用户中的%到底包不包括localhost?
  • PV统计优化设计
  • React中的“虫洞”——Context
  • spring boot 整合mybatis 无法输出sql的问题
  • 从setTimeout-setInterval看JS线程
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 近期前端发展计划
  • 两列自适应布局方案整理
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 那些被忽略的 JavaScript 数组方法细节
  • 那些年我们用过的显示性能指标
  • 数据仓库的几种建模方法
  • 我感觉这是史上最牛的防sql注入方法类
  • 找一份好的前端工作,起点很重要
  • # 达梦数据库知识点
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (4)(4.6) Triducer
  • (function(){})()的分步解析
  • (四)库存超卖案例实战——优化redis分布式锁
  • (一)Neo4j下载安装以及初次使用
  • (一)python发送HTTP 请求的两种方式(get和post )
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • (转)linux下的时间函数使用
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .Net 8.0 新的变化
  • .Net Core和.Net Standard直观理解
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET6实现破解Modbus poll点表配置文件
  • .NET连接MongoDB数据库实例教程
  • .pop ----remove 删除
  • ::什么意思
  • @SentinelResource详解
  • []利用定点式具实现:文件读取,完成不同进制之间的
  • [1127]图形打印 sdutOJ
  • [2016.7 Day.4] T1 游戏 [正解:二分图 偏解:奇葩贪心+模拟?(不知如何称呼不过居然比std还快)]
  • [20170713] 无法访问SQL Server