openLayer-动画线

本文介绍了如何在OpenLayers中实现动画线效果,包括点击事件触发的延长线、线条逐渐显示、动态动画线以及累加点的绘制,详细探讨了前端图形渲染和图像处理技术在地图交互中的应用。
摘要由CSDN通过智能技术生成

1、点击事件延长线

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1、导入openlayers的依赖 -->
    <link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css">
    <script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
    <script src="./utils/index.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        /* 3、设置一个瓦片图层 */
        const gaode = new ol.layer.Tile({
            title: "高德地图",
            source: new ol.source.XYZ({
                url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
                wrapX: false
            })
        });
        /* 4、实例化地图 */
        /* new Vue({
            el
        }) */
        const map = new ol.Map({
            /* 将实例化的地图挂载到对应的DOM元素上 */
            target: "map",
            layers: [gaode],
            /* 设置地图以中心点显示/坐标显示/方法级别显示 */
            view: new ol.View({
                center: [114.30, 30.50],
                zoom: 12,
                /* 坐标 */
                projection: "EPSG:4326"
            })

        })
        let data = {
            "type": "FeatureCollection",
            "features": [
                {
                    "type": "Feature",
                    "properties": {},
                    "geometry": {
                        "type": "LineString",
                        "coordinates": [
                            [114.300608, 30.526328],
                            [114.30629, 30.526678],
                            [114.309265, 30.524697]

                        ]
                    }
                }
            ]
        }
        let features = new ol.format.GeoJSON().readFeatures(data);
        const source = new ol.source.Vector({
            features
        })
        let layer = new ol.layer.Vector({
            source,
            style: setLineStyle({})
        })
        map.addLayer(layer);

        map.on("click", evt => {
            let position = evt.coordinate;
            console.log(position)
            let target_feature = features[0].getGeometry();
            console.log(target_feature)
            target_feature.appendCoordinate(position);
        })
    </script>
</body>

</html>

2、线慢慢显示

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1、导入openlayers的依赖 -->
    <link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css">
    <script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
    <script src="./utils/index.js"></script>
    <script src="https://lib.baomitu.com/Turf.js/latest/turf.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        /* 3、设置一个瓦片图层 */
        const gaode = new ol.layer.Tile({
            title: "高德地图",
            source: new ol.source.XYZ({
                url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
                wrapX: false
            })
        });
        /* 4、实例化地图 */
        /* new Vue({
            el
        }) */
        const map = new ol.Map({
            /* 将实例化的地图挂载到对应的DOM元素上 */
            target: "map",
            layers: [gaode],
            /* 设置地图以中心点显示/坐标显示/方法级别显示 */
            view: new ol.View({
                center: [114.30, 30.50],
                zoom: 12,
                /* 坐标 */
                projection: "EPSG:4326"
            })

        })
        let data = {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [114.302214, 30.532323],
                    [114.362718, 30.529691]
                ]
            }
        }
        let feature = new ol.format.GeoJSON().readFeature(data);
        const source = new ol.source.Vector({
            features: [feature]
        })
        const layer = new ol.layer.Vector({
            source,
            style: setLineStyle({
                color: "#ff2d5180",
                width: 8
            })
        })
        map.addLayer(layer);
        /* 设置另外一个图层 */
        /* 1、让线只显示1/10; */
        let distance = turf.lineDistance(data);
        let path = turf.along(data, distance / 10)
        let line_path = turf.lineString([[114.302214, 30.532323], path.geometry.coordinates])
        const feature_animate = new ol.format.GeoJSON().readFeature(line_path);
        const layer_animate = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: [feature_animate]
            }),
            style: setLineStyle({
                // color: "#",
                width: 4
            })
        })
        map.addLayer(layer_animate)
        /*
        let sum = 10;
        let start = 1;
        2、每次点击事件的时,都增加1/10,直到线完全显示为止
         */
        let sum = 10;
        let start = 1;
        map.on("click", () => {
            start++;
            /* 考虑设置临界值 */
            if (start > sum) {
                return;
            }
            let along_path = turf.along(data, distance * (start / sum));
            feature_animate.getGeometry().appendCoordinate(along_path.geometry.coordinates);
        })
    </script>
</body>

</html>

3、动画线

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1、导入openlayers的依赖 -->
    <link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css">
    <script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
    <script src="./utils/index.js"></script>
    <script src="https://lib.baomitu.com/Turf.js/latest/turf.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        /* 3、设置一个瓦片图层 */
        const gaode = new ol.layer.Tile({
            title: "高德地图",
            source: new ol.source.XYZ({
                url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
                wrapX: false
            })
        });
        /* 4、实例化地图 */
        /* new Vue({
            el
        }) */
        const map = new ol.Map({
            /* 将实例化的地图挂载到对应的DOM元素上 */
            target: "map",
            layers: [gaode],
            /* 设置地图以中心点显示/坐标显示/方法级别显示 */
            view: new ol.View({
                center: [114.30, 30.50],
                zoom: 12,
                /* 坐标 */
                projection: "EPSG:4326"
            })

        })
        let data = {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [114.302214, 30.532323],
                    [114.362718, 30.529691]
                ]
            }
        }
        let feature = new ol.format.GeoJSON().readFeature(data);
        const source = new ol.source.Vector({
            features: [feature]
        })
        const layer = new ol.layer.Vector({
            source,
            style: setLineStyle({
                color: "#ff2d5180",
                width: 8
            })
        })
        map.addLayer(layer);
        /* 设置另外一个图层 */
        /* 1、让线只显示1/10; */
        let distance = turf.lineDistance(data);
        let path = turf.along(data, distance / 10)
        let line_path = turf.lineString([[114.302214, 30.532323], path.geometry.coordinates])
        const feature_animate = new ol.format.GeoJSON().readFeature(line_path);
        const layer_animate = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: [feature_animate]
            }),
            style: setLineStyle({
                // color: "#",
                width: 4
            })
        })
        map.addLayer(layer_animate)
        /*
        let sum = 10;
        let start = 1;
        2、每次点击事件的时,都增加1/10,直到线完全显示为止
         */
        let sum = 100;
        let start = 1;

        function frame() {
            start++;
            /* 考虑设置临界值 */
            if (start > sum) {
                return;
            }
            let along_path = turf.along(data, distance * (start / sum));
            feature_animate.getGeometry().appendCoordinate(along_path.geometry.coordinates);
            /* 实现点坐标位置的修改 */
            setTimeout(frame, 50)

        }
        map.on("click", () => {
            frame();
        })
    </script>
</body>

</html>

4、累加点

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1、导入openlayers的依赖 -->
    <link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css">
    <script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
    <script src="./utils/index.js"></script>
    <script src="https://lib.baomitu.com/Turf.js/latest/turf.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        /* 3、设置一个瓦片图层 */
        const gaode = new ol.layer.Tile({
            title: "高德地图",
            source: new ol.source.XYZ({
                url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
                wrapX: false
            })
        });
        /* 4、实例化地图 */
        /* new Vue({
            el
        }) */
        const map = new ol.Map({
            /* 将实例化的地图挂载到对应的DOM元素上 */
            target: "map",
            layers: [gaode],
            /* 设置地图以中心点显示/坐标显示/方法级别显示 */
            view: new ol.View({
                center: [114.30, 30.50],
                zoom: 12,
                /* 坐标 */
                projection: "EPSG:4326"
            })

        })
        let data = {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [114.302214, 30.532323],
                    [114.362718, 30.529691]
                ]
            }
        }
        let marker = new ol.Feature({
            geometry: new ol.geom.Point([114.302214, 30.532323])
        })
        const marker_layer = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: [marker]
            }),
            style: setMarkerStyle("./images/定位.png")
        })
        map.addLayer(marker_layer)
        let feature = new ol.format.GeoJSON().readFeature(data);
        const source = new ol.source.Vector({
            features: [feature]
        })
        const layer = new ol.layer.Vector({
            source,
            style: setLineStyle({
                color: "#ff2d5180",
                width: 8
            })
        })
        map.addLayer(layer);
        /* 设置另外一个图层 */
        /* 1、让线只显示1/10; */
        let distance = turf.lineDistance(data);
        let path = turf.along(data, distance / 20)
        let line_path = turf.lineString([[114.302214, 30.532323], path.geometry.coordinates])
        const feature_animate = new ol.format.GeoJSON().readFeature(line_path);
        const layer_animate = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: [feature_animate]
            }),
            style: setLineStyle({
                // color: "#",
                width: 4
            })
        })
        map.addLayer(layer_animate)
        /*
        let sum = 10;
        let start = 1;
        2、每次点击事件的时,都增加1/10,直到线完全显示为止
         */
        let sum = 20;
        let start = 1;

        function frame() {
            start++;
            /* 考虑设置临界值 */
            if (start > sum) {
                start = 0;
                feature_animate.getGeometry().setCoordinates([])
                return;
            }
            let along_path = turf.along(data, distance * (start / sum));
            feature_animate.getGeometry().appendCoordinate(along_path.geometry.coordinates);
            /* 实现点坐标位置的修改 */
            marker.getGeometry().setCoordinates(along_path.geometry.coordinates)
            setTimeout(frame, 50)

        }
        map.on("click", () => {
            frame();
        })
    </script>
</body>

</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@仗剑走天涯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值