vue3.0使用leaflet
vue
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
项目地址:https://gitcode.com/gh_mirrors/vu/vue
免费下载资源
·
1、获取天地图密钥;
访问:https://www.tianditu.gov.cn/
注册并登录,访问开发资源 =》地图API =》
地图服务=》申请key
应用管理=》创建新应用=》获取到对应天地图key
2、引入leaflet组件
参考资料:https://leafletjs.com/reference.html#path
npm install leaflet
在main.ts里面引用地图样式
//引用地图样式
import "leaflet/dist/leaflet.css";
在.vue文件中加载leaflet
<template>
<div class="h-full">
<div class="h-full w-full" id="map"></div>
</div>
</template>
<script setup lang="ts">
import {ref, onMounted} from "vue";
import L from "leaflet";
function init() {
// init map
var map = new L.Map('map', {
maxZoom: 24,
minZoom: 5,
center: [37.03, 111.92],
zoom: 16
});
L.circle([37.03, 111.92], { radius: 100, color: "#ff0000", weight: 5, fill: true, fillColor: "#00ff00", fillOpacity: 1 }).addTo(map);
}
onMounted(() => {
init();
})
</script>
<style lang="scss" scoped>
</style>
效果如下:
3、加载地图和影像
引入leaflet.chinatmsproviders组件;
npm install leaflet.chinatmsproviders
<template>
<div class="h-full">
<div class="h-full w-full" id="map"></div>
</div>
</template>
<script setup lang="ts">
import {ref, onMounted} from "vue";
import L from "leaflet";
import "leaflet.chinatmsproviders";
function init() {
//天地图key
const TDT_KEY = "b523cf004dc0b0eb1d6ec8bf9d381ae5";
const normalm = L.tileLayer.chinaProvider("TianDiTu.Normal.Map", {
key: TDT_KEY,
maxZoom: 18,
minZoom: 5,
});
const normala = L.tileLayer.chinaProvider("TianDiTu.Normal.Annotion", {
key: TDT_KEY,
maxZoom: 18,
minZoom: 5,
});
const imgm = L.tileLayer.chinaProvider("TianDiTu.Satellite.Map", {
key: TDT_KEY,
maxZoom: 24,
maxNativeZoom: 18,
minZoom: 5,
});
const imga = L.tileLayer.chinaProvider("TianDiTu.Satellite.Annotion", {
key: TDT_KEY,
maxZoom: 24,
maxNativeZoom: 18,
});
const normal = L.layerGroup([normalm, normala]);
const image = L.layerGroup([imgm, imga]);
const baseLayers = {
地图: normal,
影像: image,
};
// 初始化 map
var map = new L.Map('map', {
maxZoom: 24,
minZoom: 5,
center: [37.03, 111.92],
zoom: 16,
zoomControl: false,
renderer: L.canvas({ tolerance: 16 }),
layers: [normal],
});
L.control.layers(baseLayers, {}).addTo(map).setPosition("topleft");
L.control.zoom().addTo(map).setPosition("topleft");
L.circle([37.03, 111.92], { radius: 100, color: "#ff0000", weight: 5, fill: true, fillColor: "#00ff00", fillOpacity: 1 }).addTo(map);
}
onMounted(() => {
init();
})
</script>
<style lang="scss" scoped>
</style>
效果如下:
GitHub 加速计划 / vu / vue
207.54 K
33.66 K
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:2 个月前 )
73486cb5
* chore: fix link broken
Signed-off-by: snoppy <michaleli@foxmail.com>
* Update packages/template-compiler/README.md [skip ci]
---------
Signed-off-by: snoppy <michaleli@foxmail.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 4 个月前
e428d891
Updated Browser Compatibility reference. The previous currently returns HTTP 404. 5 个月前
更多推荐
已为社区贡献4条内容
所有评论(0)