openlayers3在Vue中鼠标右键取消绘制方法

3 篇文章 0 订阅
3 篇文章 0 订阅
// 本文是介绍右键取消绘制的一种解决方式,以下代码是从其他项目代码中抽取关键部分仅供大致参考,并不是完整的实例代码,请读者注意。

//初始化map

initMap(){

        let self=this;

        ...

        self.map=addmap(self.center,self.resolutions,self.resolution,self.targetmap);

        self.addInteraction();

        ...

       self.contextmenu(self.map);

        ...

}

//绘制函数

addInteraction(){

        let self=this;

        self.draw=new ol.interaction.Draw({

                        source: self.source,

                        type: type,

                        style: new ol.style.Style({ //图层样式

                        fill: new ol.style.Fill({

                            color: 'rgba(255, 255, 255, 0.2)' //填充颜色

                        }),

                        stroke: new ol.style.Stroke({

                            color: '#ffcc33',  //边框颜色

                            width: 2  // 边框宽度

                        }),

                        image: new ol.style.Circle({

                            radius: 7,

                            fill: new ol.style.Fill({

                                color: '#ffcc33'

                            })

                        })

                    }),

                    snapTolerance:5,

        });

         var snap = new ol.interaction.Snap({

                    source: self.source,

                    edge:true,

                    vertex:true,

                    pixelTolerance:5

                });

        self.map.addInteraction(self.draw); 

        //增加图形捕捉

        self.map.addInteraction(snap);

//激活绘制

        self.draw.setActive(true);

//监听左键drawend事件的

self.draw.on('drawend',function(event){

                //自行编写活动代码

        })

}

//右键函数

contextmenu(map){

                  let self=this;

                    $(map.getViewport()).on("contextmenu", function(event){

                            event.preventDefault();

                            console.log("鼠标右键value:"+event.button);

    //右键事件点击,取消绘制活动状态

                            self.draw.setActive(false);

                    });

              },





mounted () { 

        let self = this;

        document.oncontextmenu = function(){

              return false;

        }

......

}

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue 3 + TypeScript项目使用OpenLayers,您可以按照以下步骤进行操作: 1. 安装OpenLayers:使用npm或yarn安装OpenLayers库。在命令行运行以下命令: ```bash npm install ol ``` 或 ```bash yarn add ol ``` 2. 创建一个Vue组件:在您的Vue 3项目,创建一个新的Vue组件,用于显示地图。打开一个新的`.vue`文件,例如`Map.vue`。 ```vue <template> <div ref="mapContainer" class="map-container"></div> </template> <script lang="ts"> import { defineComponent, onMounted, ref } from 'vue'; import 'ol/ol.css'; import { Map, View } from 'ol'; export default defineComponent({ name: 'Map', setup() { const mapContainer = ref(null); onMounted(() => { const map = new Map({ target: mapContainer.value, view: new View({ center: [0, 0], zoom: 2, }), }); }); return { mapContainer, }; }, }); </script> <style scoped> .map-container { width: 100%; height: 400px; } </style> ``` 3. 在需要显示地图的组件使用Map组件:在需要显示地图的父组件,导入并使用Map组件。 ```vue <template> <div> <h1>My Map</h1> <Map /> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import Map from './components/Map.vue'; export default defineComponent({ name: 'App', components: { Map, }, }); </script> ``` 现在,您可以在Vue 3 + TypeScript项目使用OpenLayers来显示地图。请注意,这只是一个基本示例,您可以进一步探索OpenLayers文档来了解更多功能和用法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兰小莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值