vue中使用leaflet加载open street map的一些使用

本文介绍如何使用Leaflet JavaScript库渲染交互式地图,包括安装配置、添加地图图层及工具控件等步骤,并展示了如何通过插件支持多种地图源。

1.leaflet 的英文文档:Documentation - Leaflet - a JavaScript library for interactive mapsicon-default.png?t=M5H6https://leafletjs.com/reference.html

2.leaflet的中文文档:Documentation - Leaflet - 一个交互式地图 JavaScript 库icon-default.png?t=M5H6https://leafletjs.cn/reference-1.8.0.html

3.首先安装npm:

npm i leaflet -S

4.引进需要编辑的地图组件中,没有全局去调用。比如说新建的OpenStreetMap.vue中:

import L from "leaflet";
import "leaflet/dist/leaflet.css";

5.leaflet通过插件是可以渲染多个第三方的地图的,目前先渲染单个地图,就是openstreetmap地图,这个地图是没有自己的官方的。

const options = {
    center: [23.061977, 113.392585], // 地图中心
    minZoom: 0,
    maxZoom: 18,
    zoom: 10,
    zoomControl: true, //禁用 + - 按钮
    doubleClickZoom: true, // 禁用双击放大
    // attributionControl: true, // 移除右下角leaflet标识
    dragging: true, //禁止鼠标拖动滚动
    boxZoom: true, //决定地图是否可被缩放到鼠标拖拽出的矩形的视图,鼠标拖拽时需要同时按住shift键.
    scrollWheelZoom: true, //禁止鼠标滚动缩放
    zoomSnap: 0.5, //地图能放缩的zoom的最小刻度尺度,默认值1
    fullscreenControl: true, //全屏控件,不显示
}
let map = L.map('mapId', options)
this.map = map;
 //添加创建地图的函数
        const createTileLayer = (map, url, options) => {
          let tileLayer = L.tileLayer(url, options);
          tileLayer.addTo(map);
          return tileLayer;
        };

createTileLayer(this.map, "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png");

不出意外,以上的代码就可以简单渲染一个openstreetmap地图了

6.下面加上些小玩意,就是地图上的小工具控件,

        首先安装:

npm i leaflet.pm -S

 然后在当前的地图组件中,引进:

import "leaflet.pm";
import "leaflet.pm/dist/leaflet.pm.css";

  在地图初始化的方法中加上以下的代码:

const option= {
    //leaflet.pm配置项
        position: "topleft",
        drawPolygon: true, // 添加绘制多边形
        drawMarker: false, //添加按钮以绘制标记
        drawCircleMarker: true, //添加按钮以绘制圆形标记
        drawPolyline: true, //添加按钮绘制线条
        drawRectangle: true, //添加按钮绘制矩形
        drawCircle: true, //  添加按钮绘制圆圈
        editMode: true, //  添加按钮编辑多边形
        dragMode: true, //   添加按钮拖动多边形
        cutPolygon: true, // 添加一个按钮以删除图层里面的部分内容
        removalMode: true, // 清除图层
}
this.map.pm.setLang("zh"); //设置语言  en, de, it, ru, ro, es, fr, pt_br, zh , nl
this.map.pm.addControls(option); //添加工具

不出意外应该有了

7.现在渲染多个地图,这边是封装了单独的地图组件,放到父组件中渲染,可以在父组件中,把某个地图的标识传个地图组件,然后监听,进行渲染不同的地图

         首先要安装插件:

npm i leaflet.chinatmsproviders -S
子组件中:
import "leaflet.chinatmsproviders";
props: {
      mapUrl: {
        type: String,
        default: "Geoq.Normal.Map",
      },
    },
watch: {
      mapUrl() {
        this.map.removeLayer(this.layers);
        this.$nextTick(() => {
          this.layers = this.chinaLayers(this.map, this.mapUrl);
        });
      },
    },
父组件中:
 <map-component :map-url="mapUrl"></map-component>
 data: ()=> ({
     mapUrl: "Geoq.Normal.Map",
      mapList: [
        {
          id: 1,
          name: "智图地图",
          url: "Geoq.Normal.Map",
        },
        {
          id: 4,
          name: "天地影像",
          url: "TianDiTu.Satellite.Map",
        },
        {
          id: 5,
          name: "午夜蓝",
          url: "Geoq.Normal.PurplishBlue",
        },
        {
          id: 6,
          name: "高德卫星地图",
          url: "GaoDe.Satellite.Map",
        },
        {
          id: 7,
          name: "高德地图",
          url: "GaoDe.Normal.Map",
        },
        {
          id: 8,
          name: "OSM地图",
          url: "OSM.Normal.Map",
        },
        {
          id: 9,
          name: "腾讯地图",
          url: "Tencent.Normal.Map",
        },
        {
          id: 10,
          name: "谷歌地图",
          url: "Google.Normal.Map",
        },
      ],
    })

最后在地图组件中的地图初始化方法中:

const chinaLayers = (map, url, option)=> {
        let chinaLeayer = L.tileLayer.chinaProvider(url, option);
        chinaLeayer.addTo(map);
        return chinaLeayer;
      };
this.layers = this.chinaLayers(this.map, this.mapUrl); //加载各种地图

不出意外的话,就会出现了多个地图的选择了:

8.先这样吧。。。

### 如何在 Vue使用 Leaflet 地图库 #### 安装依赖 为了在 Vue 项目中集成 Leaflet,首先需要安装 Leaflet 库及其样式文件。可以通过 npm 或 yarn 来完成安装。 ```bash npm install leaflet ``` 或者: ```bash yarn add leaflet ``` 此外,还需要引入 Leaflet 的 CSS 文件以便正常渲染地图界面[^1]。 #### 创建地图组件 下面是一个简单的 Vue 组件示例,展示了如何初始化并加载一张基本的地图。 ```vue <template> <div id="map-container"></div> </template> <script> import L from 'leaflet'; export default { name: 'LeafletMap', mounted() { this.initMap(); }, methods: { initMap() { const map = L.map('map-container').setView([51.505, -0.09], 13); L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', maxZoom: 18, }).addTo(map); } } }; </script> <style scoped> #map-container { height: 500px; } </style> ``` 在这个例子中,`L.map()` 方法用于创建一个新的地图实例,并通过 `setView()` 设置初始视图位置和缩放级别。随后,我们利用 `L.tileLayer()` 添加瓦片层到地图上,从而提供底图数据[^2]。 #### 高级功能扩展 如果希望进一步增强地图的功能,比如支持室内地图绘制,则可以考虑使用插件如 **leaflet-indoor** 提供的基础工具集来构建更复杂的场景应用[^4]。 对于地理信息服务的支持,可通过引入额外的第三方模块像 "Leaflet.WFS" 这样的扩展包,在标准 Leaflet 功能之上增加 WFS (Web Feature Service) 能力,允许访问远程服务器上的矢量数据资源[^3]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值