Vue工程搭建Leaflet项目第一弹:地图展示

一、创建Vue项目

1. 控制台运行命令:

vue create leaflet-app

2. 选择项目配置
  • ? Please pick a preset: Manually select features

F:\ProgramFile\WebGIS>vue create leaflet-app -----项目名
Vue CLI v4.5.15
? Please pick a preset: ---- 选则Vue版本
Default ([Vue 2] babel, eslint)
Default (Vue 3) ([Vue 3] babel, eslint)
Manually select features ---- 这里我选择自主选择

  • ? Check the features needed for your project: (Press to select, to toggle all, to invert selection)

() Choose Vue version
(
) Babel
( ) TypeScript
( ) Progressive Web App (PWA) Support
( ) Router
( ) Vuex
( ) CSS Pre-processors
(*) Linter / Formatter
( ) Unit Testing
( ) E2E Testing

二、下载Leaflet依赖

运行命令:

npm i leaflet --save

三、地图展示

1. 在main.js文件中引入leaflet和leaflet.css文件
// 引入Leaflet
import 'leaflet'
import 'leaflet/dist/leaflet.css'
2. 创建地图组件
<template>
  <div id="map-container"></div>
</template>
<script>
import L from "leaflet";
export default {
  name: "Map",
  components: {},
  created() {},
  mounted() {
    // 必须在组件挂载之后初始化地图,不然会报错 
    this.initMap();
  },
  methods: {
    initMap() {
      const map = L.map("map-container", {
        crs: L.CRS.EPSG3857,
        center: [24.886566,102.830513],
        zoom: 11
      });
      console.log("map:", map);
      L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
        attribution:
          '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>contributors'
      }).addTo(map);
    }
  }
};
</script>

<style scoped>
#map-container {
  position: absolute;
  top: 60px;
  left: 0;
  /* height: 100%; */
  width: 100%;
  bottom: 0;
}
</style>
3.地图展示

leaflet地图展示

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值