简体   繁体   English

在 OpenLayers 中绘制箭头的问题

[英]Problem with drawing arrows in OpenLayers

I would like to draw the simple arrow in OpenLayers.我想在 OpenLayers 中绘制简单的箭头。

I found some examples here:我在这里找到了一些例子:

OpenLayer 4 draw arrows on map https://openlayers.org/en/latest/examples/line-arrows.html Draw arrow without using any image in openlayers3 OpenLayer 4 在地图上绘制箭头https://openlayers.org/en/latest/examples/line-arrows.html 在 openlayers3 中不使用任何图像绘制箭头

and made some code:并制作了一些代码:

var arrowInteraction = new ol.interaction.Draw({
type: 'LineString',
source: vectorLayer.getSource(),
geometryFunction: function(coordinates, geometry) {
var geometry = coordinates.getGeometry();
var styles = [
    // linestring
    new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: '#ffcc33',
            width: 2
        })
    })
 ];

  geometry.forEachSegment(function (start, end) {
    var dx = end[0] - start[0];
    var dy = end[1] - start[1];
    var rotation = Math.atan2(dy, dx);

    var lineStr1 = new ol.geom.LineString([end, [end[0] - 200000, end[1] 
 + 200000]]);
    lineStr1.rotate(rotation, end);
    var lineStr2 = new ol.geom.LineString([end, [end[0] - 200000, end[1] 
 - 200000]]);
    lineStr2.rotate(rotation, end);

    var stroke = new ol.style.Stroke({
        color: 'green',
        width: 1
    });

    styles.push(new ol.style.Style({
        geometry: lineStr1,
        stroke: stroke
    }));
    styles.push(new ol.style.Style({
        geometry: lineStr2,
        stroke: stroke
    }));
   });

   return styles;
 }

I am getting an error:我收到一个错误:

coordinates.getGeometry is not a function坐标.getGeometry 不是函数

What can be wrong here?这里有什么问题?

My full code is here:我的完整代码在这里:

https://mktest.opx.pl/ https://mktest.opx.pl/

This might help: TypeError: Cannot read properties of undefined (reading 'id')这可能会有所帮助: TypeError:无法读取未定义的属性(读取“id”)

If I understand it right, the functions aren't called synchronized.如果我理解正确,这些函数不会被称为同步的。

In that tutorial, he catch it with:在那个教程中,他抓住了它:

 itemToForm = () => {
  if(this.item === undefined) {return}
         
  // The rest of the code
}

But you can also catch it with但你也可以用

  if(yourObjectOrValue === undefined) {// The rest of the code}

     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM