openlayer绘制线段并添加等距离箭头

箭头图片

一定得是此方向的箭头
在这里插入图片描述

核心代码

function stylefunction (feature) {
	const geometry = feature.getGeometry()
    // 轨迹地理长度
    const totalLength = geometry.getLength()
    // 像素间隔步长
    let step = 100
    // 将像素步长转实际地理距离步长
    let StepLength = step * resolution
    // 箭头总数
    let arrowNum = Math.floor(totalLength / StepLength)

    const rotations = [];
    const distances = [0];
    geometry.forEachSegment(function (start, end) {
        let dx = end[0] - start[0];
        let dy = end[1] - start[1];
        let rotation = Math.atan2(dy, dx);
        distances.unshift(Math.sqrt(dx ** 2 + dy ** 2) + distances[0]);
        rotations.push(rotation);
    });

    for(let i = 1 ;i < arrowNum; i ++) {
        let arrow_coor = geometry.getCoordinateAt(i * 1.0 / arrowNum)
        const d = i * StepLength;
        const grid = distances.findIndex((x) => x <= d);
        styles.push(
            new Style({
                geometry: new Point(arrow_coor),
                image: new Icon({
                    src: trackArrow,
                    opacity: 1,
                    anchor: [0.5, 0.5],
                    rotateWithView: true,
                    rotation: - rotations[distances.length - grid - 1],
                    scale: 0.8
                })
            })
        )
    }
}

效果图如下

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Vue.js和OpenLayers库来实现绘制折线并计算折线距离。下面是一个基本的实现示例: 首先,确保你已经安装了Vue.js和OpenLayers依赖: ```bash npm install vue openlayers ``` 然后,在你的Vue组件中,引入OpenLayers和样式表: ```javascript import 'ol/ol.css'; import { Map, View } from 'ol'; import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer'; import { OSM, Vector as VectorSource } from 'ol/source'; import { Draw, Modify } from 'ol/interaction'; import { createBox } from 'ol/interaction/Draw'; export default { mounted() { // 创建地图 const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }) ], view: new View({ center: [0, 0], zoom: 2 }) }); // 创建绘制交互 const source = new VectorSource(); const vector = new VectorLayer({ source: source }); map.addLayer(vector); const draw = new Draw({ source: source, type: 'LineString' }); map.addInteraction(draw); // 创建修改交互 const modify = new Modify({ source: source }); map.addInteraction(modify); // 计算折线距离 draw.on('drawend', (event) => { const feature = event.feature; const geometry = feature.getGeometry(); const coordinates = geometry.getCoordinates(); let distance = 0; for (let i = 1; i < coordinates.length; i++) { const coord1 = coordinates[i]; const coord2 = coordinates[i - 1]; distance += getDistance(coord1, coord2); } console.log('折线距离:', distance); }); // 计算两点之间的距离 function getDistance(coord1, coord2) { const dx = coord2[0] - coord1[0]; const dy = coord2[1] - coord1[1]; return Math.sqrt(dx * dx + dy * dy); } } } ``` 在上面的代码中,我们创建了一个地图实例,并添加了一个基本的OSM图层。然后,我们创建了一个矢量图层和一个绘制交互,允许用户绘制线段。在绘制结束时,我们计算折线的距离,并输出到控制台。 注意,我们使用了`ol/ol.css`样式表来确保地图正确显示。你可以根据自己的需求进行样式调整。 最后,在你的Vue模板中,添加一个具有指定ID的容器元素: ```html <template> <div> <div id="map"></div> </div> </template> ``` 这样,当你在浏览器中运行该Vue组件时,你将能够在地图上绘制折线,并计算折线的距离。 希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值