二维地图是使用leaflet,添加绘制功能的插件第二篇

接着上篇的继续介绍leaflet.pm的使用

1、知道经纬度绘制出形状或者线段或者园等几个的方式基本上都是一样的

 const latlngs=[

            [39.2675561,110.1201936],

            [39.2675560,110.1201936],

            [39.2675560,110.1201936],

            // [39.2675560,110.1201937],

            // [39.2675558,110.1201940],

            [39.2675557,110.1201939],

            [39.2675557,110.1201941],

            [39.2626659 ,110.1253095] ];

根据上面的经纬度数据画形状

this.polygon= L.polygon(latlngs, { color: "red" }).addTo(map);

this.polygon.bindTooltip('this is 个多边形');

上面这样的就画好了,下面的是去改变一些对应的颜色宽度等的一些属性

// color:线段颜色 // weight:线段宽度 // opacity:线段透明度 // dashArray:虚线间隔 // fill:是否填充内部(true/false) // fillColor:内部填充颜色,如不设置,默认为color颜色 // fillOpacity:内部填充透明度

const circle= [39.2675561,110.1201936]

根据上面的数据画圆 ,radius这个是代表的半径

this.circle= L.circle(circle, { radius: 200 }).addTo(this.map);

2、上面绘制的这些的开启编辑功能

this.polygon.pm.enable({
    allowSelfIntersection: false
});
this.circle.pm.enable({
    allowSelfIntersection: false
});

3、当然了绘制完了还有对应的操作方法

图层的编辑

this.polygon.on("pm:edit", obj => {
    obj.target.setStyle({ color: "orange" });
    console.log(obj.target._latlngs[0]);//这个可能就是你需要的经纬度呦
});

图层的点击双击或者键盘方法

this.polygon.on("click", e=>{ alert("点击事件") });

/*
click: 点击事件
dblclick:双击	
mousedown:按下鼠标按钮
mouseup:释放鼠标按钮
mouseover:鼠标进入
mouseout:鼠标离开
mousemove:鼠标移入
contextmenu:鼠标右键
preclick:点击之前
*/

单机过程需要双击的会引起事件冲突,解决办法就在下边了

L.polygon(latlngs, { color: "red" }).addTo(map).on('click',e=>{

timer=setTimeout(()=>{

在这里面执行你的单机需要执行的操作

})

}).on('dblclick',e=>{

  在这里面加你双击需要做的操作

  clearTimeout(timer)

})

4、加了点击方法有的时候还是需要移除你加的方法

polygon.off("click")

5、需要将你的图层置为顶层或者底层

this.polygon.bringToBack(); // 置为顶层
this.polygon.bringToFront(); // 置为底层

6、修改过程中不想要你的修改了就可以重置

this.polygon.setLatLngs([
    [40.0214690112063, 116.50239229202272],
    [40.019694293123855, 116.50224208831787],
    [40.01979288978388, 116.50580406188966],
    [40.021436146476105, 116.50601863861085],
    [40.02253710631967, 116.50316476821901]
]);
this.polygon.setStyle({ color: "red" });
this.polygon.pm.disable();

7、获取图层的边界值

this.polygon.getLatLngs()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue 3环境中,您可以使用HTML5的canvas元素和Leaflet库来绘制地图边界的经纬度刻度和数值。以下是一个简单的示例代码: ```html <template> <div> <canvas ref="mapCanvas"></canvas> </div> </template> <script> import { ref, onMounted } from 'vue' export default { name: 'Map', setup() { const mapCanvas = ref(null) onMounted(() => { const canvas = mapCanvas.value const ctx = canvas.getContext('2d') // 设置画布尺寸 canvas.width = window.innerWidth canvas.height = window.innerHeight // 绘制地图边界的经纬度刻度和数值 ctx.fillStyle = 'black' ctx.font = '12px Arial' // 绘制经度刻度和数值 for (let i = -180; i <= 180; i += 10) { const x = (i + 180) * (canvas.width / 360) ctx.beginPath() ctx.moveTo(x, 0) ctx.lineTo(x, canvas.height) ctx.fillText(i.toString(), x, 12) ctx.stroke() } // 绘制纬度刻度和数值 for (let i = -90; i <= 90; i += 10) { const y = (90 - i) * (canvas.height / 180) ctx.beginPath() ctx.moveTo(0, y) ctx.lineTo(canvas.width, y) ctx.fillText(i.toString(), 5, y + 5) ctx.stroke() } }) return { mapCanvas } } } </script> ``` 在上面的代码中,我们使用`ref`来创建一个对canvas元素的引用`mapCanvas`,然后在组件渲染后的`onMounted`钩子函数中获取canvas的上下文对象`ctx`,设置画布尺寸,并使用`ctx.beginPath()`、`ctx.moveTo()`、`ctx.lineTo()`、`ctx.fillText()`和`ctx.stroke()`方法来绘制刻度和数值。 请注意,上面的示例仅绘制了经度和纬度的刻度和数值,如果您需要绘制地图边界的其他元素,可以根据Leaflet库提供的相关方法进行扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值