【Leaflet.js实战】路径绘制与多边形:L.Polyline/L.Polygon/L.Circle 完整实现指南

前言:路径和多边形是地图应用的重要功能。本文将深入讲解Leaflet中L.Polyline、L.Polygon、L.Circle的使用方法,从基础绘制到高级编辑,助你掌握地图几何图形处理。

一、L.Polyline 路径绘制

1.1 基础路径创建

// 简单路径绘制
const polyline = L.polyline([
    [39.9042, 116.4074], // 起点
    [39.9142, 116.4174], // 中间点
    [39.9242, 116.4274]  // 终点
]).addTo(map);

// 带样式的路径
const styledPolyline = L.polyline([
    [39.9042, 116.4074],
    [39.9142, 116.4174],
    [39.9242, 116.4274]
], {
   
   
    color: 'red',        // 线条颜色
    weight: 5,           // 线条宽度
    opacity: 0.8,        // 透明度
    smoothFactor: 1.0,   // 平滑因子
    lineCap: 'round',    // 线条端点样式
    lineJoin: 'round'   // 线条连接样式
}).addTo(map);

1.2 路径样式配置

// 不同样式的路径配置
const pathStyles = {
   
   
    highway: {
   
   
        color: '#ff6b6b',
        weight: 6,
        opacity: 0.8,
        lineCap: 'round',
        lineJoin: 'round'
    },
    street: {
   
   
        color: '#4ecdc4',
        weight: 3,
        opacity: 0.7,
        lineCap: 'round',
        lineJoin: 'round'
    },
    walking: {
   
   
        color: '#45b7d1',
        weight: 2,
        opacity: 0.6,
        dashArray: '10, 10', // 虚线样式
        lineCap: 'round'
    }
};

// 应用样式
const highwayPath = L.polyline(coordinates, pathStyles.highway).addTo(map);
const streetPath = L.polyline(coordinates, pathStyles.street).addTo(map);
const walkingPath = L.polyline(coordinates, pathStyles.walking).addTo(map);

1.3 路径动画效果

// 路径动画实现
class AnimatedPolyline {
   
   
    constructor(map, coordinates, options = {
    
    }) {
   
   
        this.map = map;
        this.coordinates = coordinates;
        this.options = {
   
   
            color: '#ff6b6b',
            weight: 4,
            opacity: 0.8,
            duration: 2000,
            ...options
        };
        this.polyline = null;
    }
    
    // 开始动画
    animate() {
   
   
        this.polyline = L.polyline([], this.options).addTo(this.map);
        
        const startTime = Date.now();
        const totalPoints = this.coordinates.length;
        
        const animate = () => {
   
   
            const elapsed = Date.now() - startTime;
            const progress = Math.min(elapsed / this.options.duration, 1);
            const currentPoints = Math.floor(progress * totalPoints);
            
            const currentPath = this.coordinates.slice(0, currentPoints + 1);
            this.polyline.setLatLngs(currentPath);
            
            if (progress < 1) {
   
   
                requestAnimationFrame(animate);
            }
        };
        
        animate();
    }
    
    // 停止动画
    stop() {
   
   
        if (this.polyline) {
   
   
            this.map.removeLayer(this.polyline);
        }
    }
}

// 使用路径动画
const animatedPath = new AnimatedPolyline(map, routeCoordinates, {
   
   
    color: '#4CAF50',
    weight: 3,
    duration: 3000
});
animatedPath.animate();

二、L.Polygon 多边形绘制

2.1 基础多边形创建

// 简单多边形
const polygon = L.polygon([
    [39.9042, 116.4074], // 顶点1
    [39.9142, 116.4074], // 顶点2
    [39.9142, 116.4174], // 顶点3
    [39.9042, 116.4174]  // 顶点4
]).addTo(map);

// 带样式的多边形
const styledPolygon = L.polygon([
    [39.9042, 116.4074],
    [39.9142, 116.4074],
    [39.9142, 116.4174],
    [39.9042, 116.4174]
], {
   
   
    color: '#ff6b6b',      // 边框颜色
    weight: 2,             // 边框宽度
    opacity: 0.8,          // 边框透明度
    fillColor: '#ff6b6b',  // 填充颜色
    fillOpacity: 0.3,      // 填充透明度
    lineCap: 'round',      // 边框端点样式
    lineJoin: 'round'      // 边框连接样式
}).addTo(map);

2.2 复杂多边形(带洞)

// 带洞的多边形
const polygonWithHole = L.polygon([
    // 外边界
    [
        [39.9042, 116.4074],
        [39.9142, 116.4074],
        [39.9142, 116.4174],
        [39.9042, 116.4174]
    ],
    // 内洞
    [
        [39.9072, 116.4104],
        [39.9112, 116.4104],
        [39.9112, 116.4144],
        [39.9072, 116.4144]
    ]
], {
   
   
    color: '#ff6b6b',
    weight: 2,
    opacity: 0.8,
    fillColor: '#ff6b6b',
    fillOpacity: 0.3
}).addTo(map);

2.3 多边形面积计算

// 面积计算工具
class PolygonCalculator {
   
   
    // 计算多边形面积(平方米)
    static calculateArea(coordinates) {
   
   
        let area = 0;
        const n = coordinates.length;
        
        for (let i = 0; i < n; i++) {
   
   
            const j = (i + 1) % n;
            area += coordinates[i][1] * coordinates[j][0];
            area -= coordinates[j][1] * coordinates[i][0];
        }
        
        area = Math.abs(area) / 2;
        
        
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

立方世界

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

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

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

打赏作者

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

抵扣说明:

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

余额充值