Vue-CLI and Leaflet(2):地图基本操做(放大,缩小,平移,定位等)

1、 功能分析

​ 接着上一篇文章。地图加载成功以后,接下来要开始对对地图的常见功能的实现,一个地图的基本功能包括:地图显示平移放大缩小定位 等功能。javascript

2、实现思路

1)平移

​ 一般 WebGIS 中地图平移是最为基本且经常使用的功能,地图会默认开启平移功能。一般状况下都不须要开发者本身去实现 平移的功能。vue

2)放大与缩小

​ 放大,缩小也同样,地图一般会默认开启此项功能。而大多数状况下,WebGIS 库会提供一些如图所示的控件来实现一级一级地放大的功能。所以, 放大与缩小实现的办法有两种。java

  1. 自带的控件git

    L.map('map', {
     zoomControl: true,
     scrollWheelZoom:true //默认开启鼠标滚轮缩放
    });
    复制代码
  2. 经过地图的API实现github

    // 逐级放大, delta 默认 1
    map.zoomIn( ?delta )
    
    // 逐级缩小, delta 默认 1
    map.zoomOut( ?delta )
    复制代码

三)代码实现

1)自带的控件

实现很简单,默认状况下 Leaflet地图对象的 zoomControl 是开启的。一般状况下时须要将 zoomControl 关闭。在咱们的工程中则应该工程的这个位置去控制:bash

// src/views/Map.vue

<template>
  <div class="map-container" id="map-container"></div>
</template>

<script>
// @ is an alias to /src

export default {
  name: "mapView",
  components: {},
  data() {
    return {
      map: null,
      OSMUrl: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    };
  },
  mounted() {
    this.map = this.$utils.map.createMap("map-container", {
      zoomControl: true
    });
    
    this.$utils.map.createTileLayer(this.map, this.OSMUrl, {});
    this.map.setView([51.505, -0.09], 13);
  }
};
</script>
<style lang="less">
.map-container {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}
</style>
复制代码

2) 自定义放大缩小功能

​ 大多数时候,项目中为了配合总体的UI风格,须要实现自定义控件的样式来实现缩放工。所以,这里结合Vuejs的特性,我自定义了一个缩放功能的组件。组件的功能包括,放大,缩小,以及回到初始化点。less

// src/components/NavigationCtrl.vue
<template>
  <div class="map-navigation">
    <ul>
      <li @click="$emit('zoomIn')">+</i>
      <li @click="$emit('resetMap')">·</i>
      <li @click="$emit('zoomOut')">-</i>
    </ul>
  </div>
</template>

<script>
export default {
  name: "mapNavigation"
};
</script>
<style lang="less">
.map-navigation {
  position: absolute;
  left: 15px;
  top: 15px;
  z-index: 999;

  width: 30px;
  box-shadow: 0px 0px 50px 2px rgba(0, 0, 0, 0.35);
  background-color: #fff;
  ul {
    padding: 0;
    margin: 0;
    list-style: none;

    li {
      width: 30px;
      height: 28px;
      font-size: 16px;
      line-height: 28px;
      cursor: pointer;
    }

    li:hover {
      background-color: rgb(212, 224, 246);
    }
  }
}
</style>
复制代码

而后在 map.vue 引用组件,这里须要注意 Vuejs 中父组件与子组件之间的事件绑定:post

// src/views/Map.vue

<template>
  <div class="map-container">
    <div id="map-container"></div>
    <NavigationCtrl @zoomIn="zoomIn" @zoomOut="zoomOut" @resetMap="resetMap"></NavigationCtrl>
  </div>
</template>

<script>
// @ is an alias to /src
import NavigationCtrl from "@/components/NavigationCtrl.vue";

export default {
  name: "mapView",
  components: { NavigationCtrl },
  data() {
    return {
      map: null,
      OSMUrl: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    };
  },
  mounted() {
    this.map = this.$utils.map.createMap("map-container", {
      zoomControl: false
    });
    this.$utils.map.createTileLayer(this.map, this.OSMUrl, {});
    this.map.setView([51.505, -0.09], 13);
  },
  methods: {
    zoomIn() {
      this.map.zoomIn();
    },
    zoomOut() {
      this.map.zoomOut();
    },
    resetMap() {
      // 
      this.map.setView([51.505, -0.09], 13);
    }
  }
};
</script>
<style lang="less">
.map-container {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}
#map-container {
  width: 100%;
  height: 100%;
}
</style>
复制代码

这样基本上开始提到的 地图显示平移放大缩小定位 就算都完成了。最后一个定位除了使用 setView 方法以外,也可用根据具体的需求采用panTo, flyTo等方法得到更好的交互效果。ui

panTo flyTo

四)总结

以上就是第二章的所有内容,这些功能过度简单基础,显得有点无聊。可WebGIS地图中不少实际业务中的功能都是一些简单的功能组合而成。后续的博文中也会涉及到部分复杂的功能,请看到的各位不吝赐教。this




目录

(一) Vue-CLI and Leaflet:起步 - 在 Vue-CLI 中使用 Leaflet

(二) Vue-CLI and Leaflet:地图基本操做(放大,缩小,平移,定位等)

(三) Vue-CLI and Leaflet: 添加 marker, polyline, polygon

(四) Vue-CLI and Leaflet: 添加 tooltips 和 popup

(五) Vue-CLI and Leaflet: 点 绘制

(六) Vue-CLI and Leaflet: 线 绘制

(七) Vue-CLI and Leaflet: 面 绘 制

(八) Vue-CLI and Leaflet :加载 Esri ArcGIS Map Service

(九) Vue-CLI and Leaflet: 图层控制基本功能的实现

(十) Vue-CLI and Leaflet: AGS 属性查询与点图查询

(十一)Vue-CLI and Leaflet: 点聚合 Leaflet.markercluster

源码请参看 个人GitHub,因为文章是一边coding,一边写的因此 Github 里面的源码可能有点乱,能够根据功能来找对应的代码。后面会陆续整理完善。

相关文章
相关标签/搜索