leaflet绘制区域(仿高德地图效果)

leaflet官网:http://leafletjs.com/

效果:
图片描述

脚本:

var map = L.map('map', {
    center: [25.1026993454,102.9312515259], // 地图中心点(昆明)
    zoom: 16, // 地图缩放层级
    zoomControl: false, // 缩放
    doubleClickZoom: false, // 禁止双击放大
    attributionControl: false // 版权
});
var kmgLayer = L.tileLayer.wms('wms切片图层地址', {
    layers: 'airport:kmg',
    format: 'image/jpeg',
    transparent: false
});
map.addLayer(kmgLayer);

// 绘制区域
var layerObj = {};
function drawPolygon() {
    var points = [],
        points_length = 0,
        polyline,
        polygon;
    // 单击
    var clickFlag,
        clickTimes = 1,
        isDrag = false;
    map.on('mousedown', function(e) {
        map.off('mousemove');
        if(clickFlag) clearTimeout(clickFlag);
        clickFlag = setTimeout(function() {
            if(clickTimes==1 && !isDrag) {
                points.push([e.latlng.lat, e.latlng.lng]);
                points_length = points.length;
                // 移动
                map.on('mousemove', function(e) {
                    // 清除
                    if(polyline) map.removeLayer(polyline);
                    if(polygon) map.removeLayer(polygon);
                    // polyline
                    points[points_length] = [e.latlng.lat, e.latlng.lng];
                    polyline = new L.Polyline(points);
                    map.addLayer(polyline);
                    // polygon
                    polygon = new L.Polygon(points);
                    map.addLayer(polygon);
                });
            }
        }, 300);
    });
    // 双击
    map.on('dblclick', function() {
        if(points.length) {
            clickTimes = 2;
            // polyline
            polyline = new L.Polyline(points);
            map.addLayer(polyline);
            // polygon
            polygon = new L.Polygon(points);
            map.addLayer(polygon);
            // 移除事件
            map.off('mousemove');
            setTimeout(function() {
                clickTimes = 1;
                // 保存layer(方便后面删除)
                var layerName = prompt('请入名称');
                if(layerName) {
                    layerObj[layerName] = [polyline, polygon];
                    console.log(layerObj);
                }
                points = [];
            }, 300);
        }
    });
    // 键盘事件
    $(document).keyup(function(e) {
        if(e.keyCode == 27) {// esc键
            if(points.length) {
                map.off('mousemove');
                clickTimes = 1;
                map.removeLayer(polyline);
                map.removeLayer(polygon);
                points = [];
            }
        }
    });
    // 拖动
    map.on('movestart', function() {
        isDrag = true;
    });
    map.on('moveend', function() {
        isDrag = false;
    });
}
drawPolygon();
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值