OpenLayers 3自定义地图工具条(一)

对于一个WEB地图应用来说,地图工具条可以说是不可或缺的一个部分,但由于一些样式、功能往往并不能满足实际项目需要,所以仍要对此部分进行定制,从这里将说明如何利用Oplenayers 3根据项目要求做一个定制化的工具条。


1、工具条功能内容内容

  • 全屏、取消全屏
  • 视图放大、缩小
  • 全图【全图范围,初始化地图到默认的层级,以及预先设定的中心点】
  • 地图标绘【点、线、面】
  • 地理定位【省份定位】
  • 地图切换【影像地图、地形地图】

2、最终工具条效果图

效果图

3、具体实现

构建页面HTML元素

<div class="tool-container">
    <div class="t_close"></div>
    <ul>
        <li id="tfullscreen">
            <span class="t_1"></span><i>全屏</i>
        </li>
        <b></b>
        <li id="tfullmap">
            <span class="t_2"></span><i>最大</i>
        </li>
        <b></b>
        <li id="tzoomin">
            <span class="t_3">
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 32
    评论
要实现类似ArcGIS绘图工具条自定义绘图控件,可以借助Vue.js和OpenLayers库来完成。 首先,在Vue.js中创建一个组件,用于包含OpenLayers地图。然后,使用OpenLayers的绘图工具来实现绘图功能。可以创建一个工具条组件,在该组件中包含各种绘图工具,比如点、线、面等。 下面是一个简单的实现示例: ``` html <template> <div class="map"> <div id="map" class="map-container"></div> <div class="toolbar"> <button @click="activateDraw('Point')">绘制点</button> <button @click="activateDraw('LineString')">绘制线</button> <button @click="activateDraw('Polygon')">绘制多边形</button> </div> </div> </template> <script> import 'ol/ol.css' import { Map, View } from 'ol' import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer' import { OSM, Vector as VectorSource } from 'ol/source' import { Draw, Modify, Snap } from 'ol/interaction' export default { name: 'MapComponent', data() { return { map: null, draw: null, modify: null, snap: null } }, mounted() { this.initMap() }, methods: { initMap() { this.map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }), new VectorLayer({ source: new VectorSource() }) ], view: new View({ center: [0, 0], zoom: 2 }) }) this.initDraw() }, initDraw() { const source = this.map.getLayers().item(1).getSource() this.draw = new Draw({ source: source, type: 'Point' }) this.modify = new Modify({ source: source }) this.snap = new Snap({ source: source }) this.map.addInteraction(this.draw) this.map.addInteraction(this.modify) this.map.addInteraction(this.snap) this.draw.on('drawend', (event) => { const feature = event.feature feature.setId(new Date().getTime()) }) }, activateDraw(type) { this.draw.setActive(true) this.draw.setType(type) this.modify.setActive(true) this.snap.setActive(true) } } } </script> <style scoped> .map { position: relative; height: 400px; } .map-container { height: 100%; width: 100%; } .toolbar { position: absolute; top: 10px; left: 10px; z-index: 1; } </style> ``` 上面的代码中,首先初始化了一个OpenLayers地图,在地图上添加了OSM图层和一个空白的矢量图层,用于存储绘制的要素。然后,使用Draw、Modify和Snap三个交互对象来实现绘图功能。在initDraw方法中初始化这些交互对象,并在activateDraw方法中根据用户选择的绘图类型来设置Draw交互对象的type属性。 在模板中,使用三个按钮来触发activateDraw方法,分别对应绘制点、线和多边形。同时,使用CSS样式将绘图工具条定位地图左上角。 这样就完成了一个简单的自定义绘图控件,类似于ArcGIS的绘图工具条。可以根据自己的需要进行二次开发,添加更多的绘图工具和样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值