简体   繁体   English

Openlayers如何设置绘制的线串的样式与要绘制的线串的样式不同

[英]Openlayers how do you style drawn linestring different than what you are about to draw

I have a openlayers map which has draw interaction. 我有一个具有绘图交互作用的openlayers地图。 When user starts drawing a linestring on the map, the portion of linestring drawn should look different than what the user would draw next. 当用户开始在地图上绘制线串时,绘制的线串部分应看起来与用户接下来要绘制的部分不同。

In brief when user drops a point on the map, the line till that point should be block, without dash or any other styling options. 简而言之,当用户在地图上放置一个点时,到该点的线应为块状,没有破折号或任何其他样式选择。

To illustrate what I am trying to do here goes the screenshot - 为了说明我要在此处执行的操作,请截图-

预期产量

We can see user is about to add another point on the line so line till that point should turn block in blue color. 我们可以看到用户将要在直线上添加另一个点,因此直到该点的直线应变为蓝色块。

I have maintained the collection of points being added to the map, when user opts to delete the last point, it is being removed from the map but the last segment should also disappear from map. 我维护了要添加到地图上的点的集合,当用户选择删除最后一个点时,该点将从地图上删除,但最后一个线段也应该从地图上消失。 Can't find anything to achieve that. 找不到任何可以实现的目标。

Also I have added the ol.style.RegularShape() to display a square but it is displaying a circle instead don't know what I am doing wrong. 我还添加了ol.style.RegularShape()来显示正方形,但它显示的是圆形,而不知道我在做什么错。

Here is jsbin link to my code - Draw interaction line style problem 这是我的代码的jsbin链接- 绘制交互线条样式问题

You would need to use a style function for your draw style and divide the geometry into two parts for styling: 您需要为绘画样式使用样式功能,并将几何分为两部分进行样式设置:

var drawStyle = function(feature) {
    var styles = [];
    if (feature.getGeometry().getType() == 'LineString') {
        var coordinates = feature.getGeometry().getCoordinates();
        styles.push(new ol.style.Style({
            geometry: new ol.geom.LineString(coordinates.slice(-2)),
            stroke: new ol.style.Stroke({
                color: '#0000FF',
                lineDash: [1, 20],
                width: 5,
              lineCap:'square',
              lineJoin:'round',
              lineDashOffset:10
            })
        })); 
        styles.push(new ol.style.Style({
            geometry: new ol.geom.MultiPoint(coordinates),
            image: new ol.style.RegularShape({
              points: 4,
              radius: 6,
              angle: Math.PI / 4,
              fill: new ol.style.Fill({
                color: 'blue'
              }),
              stroke: new ol.style.Stroke({
                color: 'blue'
              }),
            })
        })); 
        if (coordinates.length > 2) {
          styles.push(new ol.style.Style({
            geometry: new ol.geom.LineString(coordinates.slice(0,-1)),
            stroke: new ol.style.Stroke({
                color: '#0000FF',
                width: 2,
              lineCap:'square',
              lineJoin:'round'
            })
          }));
        }
    }
    return styles;
}

To remove the last point from the line interaction simply use 要从线互动中删除最后一点,只需使用

line.removeLastPoint();

making sure var line; 确保var line; is declared so it is in the scope of the button click function 被声明,因此它在按钮单击功能的范围内

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

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