基于Leaflet实现图标旋转角度的效果(附源代码下载)

前言

本文将详细描述如何基于Leaflet开发库实现Marker图标旋转角度的应用效果,如下图所示。


一、开发技术

Leaflet开发库:Leaflet 1.9.3

图标旋转插件:下载地址为:Leaflet Rotated Marker


二、实现思路

1、下载Leaflet Rotated Marker插件:

传统的开发方式可直接下载相应的js和css文件到本地;

使用Node.js开发方式可通过 npm install leaflet-rotatedmarker 命令安装即可。

2、核心实现思路

安装Leaflet Rotated Marker插件后,同样,使肜L.marker添加图标,同时通过rotationAngle属性添加需要旋转的角度,如下所示。

//添加图标
L.marker([48.8631169, 2.3708919], {
    rotationAngle: 45  //需要旋转的角度
}).addTo(map);

三、实现代码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <title>Leaflet地图应用开发高级篇</title>
    <!--引用leaflet样式-->
    <link href="./static/css/leaflet.css" rel="stylesheet" />

    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
        }

        html,
        body,
        #l_map {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <div id="l_map"></div>
    <!--引用leaflet库-->
    <script type="text/javascript" src="./static/js/leaflet.js"></script>
    <!--图标旋转插件-->
    <script src="./static/plugins/Leaflet.RotatedMarker-master/leaflet.rotatedMarker.js"></script>
    <script type="text/javascript">
        //使用严格模式
        "use strict";
        var map = null;
        /**初始化地图显示
        */
        function initMap() {
            //地图容器
            map = L.map('l_map', {
                //参考坐标系
                crs: L.CRS.EPSG3857,
                //显示中心
                center: [22.27, 113.57],
                //最小显示等级
                minZoom: 1,
                //最大显示等级
                maxZoom: 18,
                //当前显示等级
                zoom: 8,
                //设置缩放控件不显示
                zoomControl: false,
                noWrap: true,
                //设置版权属性值的显示内容
                attribution: ''
            });
            var Esri_WorldImagery = L.tileLayer('http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
                attribution: ''
            });
            //初始时加载天地图影像图层组
            map.addLayer(Esri_WorldImagery);

            //定义一个地图缩放控件
            var zoomControl = L.control.zoom({ position: 'bottomright' });
            //将地图缩放控件加入到地图容器中
            map.addControl(zoomControl);

            addMarker();

        }

        function addMarker() {
            var icon = L.icon({
                //图标地址
                iconUrl: "./static/images/ico_plant.png",
                //图标大小
                iconSize: [48, 48]
            });

            var marker1 = L.marker([22.27, 113.57], {
                rotationAngle: 120,
                icon: icon,
                draggable: false
            });
            marker1.addTo(map);

            var marker2 = L.marker([21.27, 114.57], {
                rotationAngle: 135,
                icon: icon,
                draggable: false
            });
            marker2.addTo(map);

            var marker3 = L.marker([21.27, 115.57], {
                rotationAngle: 90,
                icon: icon,
                draggable: false
            });
            marker3.addTo(map);

            var marker4 = L.marker([21.80, 116.57], {
                rotationAngle: 60,
                icon: icon,
                draggable: false
            });
            marker4.addTo(map);

            var marker4 = L.marker([22.80, 117.57], {
                rotationAngle: 0,
                icon: icon,
                draggable: false
            });
            marker4.addTo(map);
        }
        initMap();
    </script>
</body>
</html>

四、源代码下载

本文描述的功能效果实现源代码可通过此链接下载:基于Leaflet实现图标旋转角度效果的源代码 icon-default.png?t=M85Bhttps://download.csdn.net/download/liu2004051957/87283604

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很高兴为您服务!以下是基于Vue框架和Leaflet实现的轨迹回放的示例代码: <template> <div id="map"></div> </template> <script> import L from "leaflet"; import "leaflet-polylinedecorator"; export default { data() { return { map: null, polyline: null, marker: null, positions: [ [39.9078, 116.3975], [31.2304, 121.4737], [22.3964, 114.1095], [35.6895, 139.6917], [37.5665, 126.9780] ], currentIndex: 0 }; }, mounted() { this.initMap(); this.initPolyline(); this.initMarker(); this.play(); }, methods: { initMap() { this.map = L.map("map").setView([39.9078, 116.3975], 3); L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { attribution: "Map data © OpenStreetMap contributors" }).addTo(this.map); }, initPolyline() { const polylineOptions = { color: "blue", weight: 5, opacity: 0.7 }; this.polyline = L.polyline(this.positions, polylineOptions).addTo(this.map); const arrowOptions = { patterns: [ { offset: "50%", repeat: 100, symbol: L.Symbol.arrowHead({ pixelSize: 15, polygon: false, pathOptions: { color: "blue", stroke: true, weight: 3 } }) } ] }; L.polylineDecorator(this.polyline, arrowOptions).addTo(this.map); }, initMarker() { const markerOptions = { icon: L.icon({ iconUrl: require("@/assets/marker.png"), iconSize: [32, 32], iconAnchor: [16, 16] }), zIndexOffset: 1000 }; this.marker = L.marker(this.positions[0], markerOptions).addTo(this.map); }, play() { setInterval(() => { if (this.currentIndex < this.positions.length - 1) { this.currentIndex++; this.marker.setLatLng(this.positions[this.currentIndex]); this.map.panTo(this.positions[this.currentIndex]); } }, 1000); } } }; </script> <style> #map { height: 500px; } </style> 请注意,该示例代码仅实现了轨迹回放功能,未进行样式和交互优化。如果您有其他需求,可以根据该示例代码进行相应的修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

动立信息服务

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

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

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

打赏作者

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

抵扣说明:

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

余额充值