vue2中使用Leaflet

第一步:安装依赖

项目中需要安装两个插件,依次安装即可;

npm install vue2-leaflet --save
npm install leaflet --save

第二步:在main.js中引入

//main.js

import 'leaflet/dist/leaflet.css'



//为了保证后续地图的正常加载,修改icon的路径
/* leaflet icon */
delete L.Icon.Default.prototype._getIconUrl
L.Icon.Default.mergeOptions({
  iconRetinaUrl: require('leaflet/dist/images/marker-icon-2x.png'),
  iconUrl: require('leaflet/dist/images/marker-icon.png'),
  shadowUrl: require('leaflet/dist/images/marker-shadow.png'),
})

第三步:新建页面

//leafletView页面

<template>
  <div class="leaflet">
    <div id="map" class="mapStyle" style="z-index: 10"></div>
  </div>
</template>

<script>
import L from "leaflet";

export default {
  data() {
    return {
      map: null,
      center: [30.524, 119.267],
      zoom: 5,
    };
  },

  mounted() {
    this.init();
  },

  methods: {
    // 初始化地图
    init() {
      this.map = L.map("map", { attributionControl: false }).setView(
      //可以调节数值的大小来决定地图显示的位置,5是最初放大倍数,数值越大,放大倍数越大
        [35.04656, 104.86254],
        5
      );
      L.tileLayer(
        "https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
        {
        //最大放大倍数是maxZoom的值
          maxZoom: 19,
          attribution:
            '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
        }
      ).addTo(this.map);
    },

  },
};
</script>


<style scoped>
* { 
  padding: 0;
  margin: 0;
}
.mapStyle {
  width: 100%;
  /* height: 100%; */
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
</style>

结果展示:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值