Leaflet中文网
效果图
鼠标左键按下—>拖动开始绘制区域–>松开绘制完成 (效果跟window桌面拖动出现的框框一样) 😃
直接上代码
initMap () {
this.map = L.map('chinaMap', {
layers: [
new L.TileLayer("https://webst01.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}")
],
center: [39.77642556850833, 116.41113281250001],
zoom: 4,
minZoom: 2,
zoomControl: false,
attributionControl: false
});
this.map.on('mousedown',e=>{
if(!event.button){
this.map.dragging.disable();
this.juxing.startPoint = e.latlng;
this.map.on('mousemove',m=>{
this.juxing.endPoint = m.latlng;
if(this.juxing.object!=null){
this.juxing.object.remove();
}
this.juxing.object = L.rectangle([
this.juxing.startPoint,this.juxing.endPoint
], {color: "#ff7800", weight: 1}).addTo(this.map);
})
}
});
this.map.on('mouseup',e=>{
if(!event.button){
this.map.dragging.enable();
this.juxing.object.remove();
console.log("开始位置",this.juxing.startPoint,this.juxing.endPoint);
this.map.off('mousemove');
}
})
}
|