一、 功能分析
接着上一篇文章。地图加载成功之后,接下来要开始对对地图的常见功能的实现,一个地图的基本功能包括:地图显示,平移,放大,缩小,定位 等功能。
二、实现思路
1)平移
通常 WebGIS 中地图平移是最为基本且常用的功能,地图会默认开启平移功能。通常情况下都不需要开发者自己去实现 平移的功能。
2)放大与缩小
放大,缩小也一样,地图通常会默认开启此项功能。而大多数情况下,WebGIS 库会提供一些如图所示的控件来实现一级一级地放大的功能。因此, 放大与缩小实现的办法有两种。
-
自带的控件
L.map('map', { zoomControl: true, scrollWheelZoom:true //默认开启鼠标滚轮缩放 }); -
通过地图的API实现
// 逐级放大, delta 默认 1 map.zoomIn( ?delta ) // 逐级缩小, delta 默认 1 map.zoomOut( ?delta )
三)代码实现
1)自带的控件
实现很简单,默认情况下 Leaflet地图对象的 zoomControl 是开启的。通常情况下时需要将 zoomControl 关闭。在我们的工程中则应该工程的这个位置去控制:
// 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的特性,我自定义了一个缩放功能的组件。组件的功能包括,放大,缩小,以及回到初始化点。
// 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 中父组件与子组件之间的事件绑定:
// 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等方法获得更好的交互效果。
| panTo | flyTo |
|---|---|
四)总结
以上就是第二章的全部内容,这些功能过分简单基础,显得有点无聊。可WebGIS地图中很多实际业务中的功能都是一些简单的功能组合而成。后续的博文中也会涉及到部分复杂的功能,请看到的各位不吝赐教。
目录
(一) 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 :加载 Esri ArcGIS Map Service
(九) Vue-CLI and Leaflet: 图层控制基本功能的实现
(十) Vue-CLI and Leaflet: AGS 属性查询与点图查询
(十一)Vue-CLI and Leaflet: 点聚合 Leaflet.markercluster
源码请参看 我的GitHub,由于文章是一边coding,一边写的所以 Github 里面的源码可能有点乱,可以根据功能来找对应的代码。后面会陆续整理完善。