简体   繁体   English

如何在OpenLayer3中移动lineString?

[英]How can i move a lineString in OpenLayer3?

I have to create buttons that enable me to move and rotate the my lineString. 我必须创建使我能够移动和旋转lineString的按钮。

This is my code: 这是我的代码:

var raster = new ol.layer.Tile({source: new ol.source.MapQuest({layer: 'sat'})});
var lineFeature = new ol.Feature(
new ol.geom.LineString([[-1e7, 1e6], [-1e6, 3e6]]));
var vector= new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [lineFeature]
  }),
  style: new ol.style.Style({
    image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
      anchor: [0.5, 46],
      anchorXUnits: 'fraction',
      anchorYUnits: 'pixels',
      opacity: 0.95,
    })),
    stroke: new ol.style.Stroke({
      width: 3,
      color: [255, 0, 0, 1]
    }),
    fill: new ol.style.Fill({
      color: [0, 0, 255, 0.6]
    })
  })
})
var map = new ol.Map({
 layers: [raster,vector],
  target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
 })
});

I have to create 4 buttons that allow me to move the lineString in the 4 direction , how can i do this? 我必须创建4个按钮,允许我在4个方向上移动lineString,我该怎么做?

You can change the geometry of your line fetaures depending on the type of move. 您可以根据移动类型来更改线型的几何形状。 And do the same for the rotation. 并为旋转做同样的事情。

here is the html to do so: 这是这样做的HTML:

<div  id="map"></div>
<div>
 <button id="movemeup" >move up</button> 
 <button id="movemedown">move down</button>
 <button id="movemeright">move right</button>
 <button id="movemeleft">move left</button>
 <button id="rotate">rotate</button>
</div>

and your javascript (note that I use var stepMove = 1000000; which express how many meters to move) . 和您的JavaScript(请注意,我使用var stepMove = 1000000;表示要移动多少米)。 For the rotation I was inspired from this 对于旋转,我从中得到了启发

   var raster = new ol.layer.Tile({source: new ol.source.MapQuest({layer: 'sat'})});
var lineFeature = new ol.Feature(
new ol.geom.LineString([[-1e7, 1e6], [-1e6, 3e6]])
);
var vector= new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [lineFeature]
  }),
  style: new ol.style.Style({
    image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
      anchor: [0.5, 46],
      anchorXUnits: 'fraction',
      anchorYUnits: 'pixels',
      opacity: 0.95,
    })),
    stroke: new ol.style.Stroke({
      width: 3,
      color: [255, 0, 0, 1]
    }),
    fill: new ol.style.Fill({
      color: [0, 0, 255, 0.6]
    })
  })
});

var map = new ol.Map({
 layers: [raster,vector],
  target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
 })
});


var stepMove = 1000000;
document.getElementById('movemeup').onclick = function (){
    moveit('up')
};
document.getElementById('movemedown').onclick = function (){
    moveit('down')
};
document.getElementById('movemeright').onclick = function (){
    moveit('right')
};
document.getElementById('movemeleft').onclick = function (){
    moveit('left')
};

document.getElementById('rotate').onclick = function (){
    rotateFeat();
};


function moveit(dir){

var lineCoords = lineFeature.getGeometry().getCoordinates();
var newLineCoords = new Array();
if (dir === 'up'){
    for(var i=0;i<lineCoords.length;i++){
    newLineCoords.push([lineCoords[i][0],lineCoords[i][1]+stepMove]);
    }
} else if (dir === 'down'){
    for(var i=0;i<lineCoords.length;i++){
    newLineCoords.push([lineCoords[i][0],lineCoords[i][1]-stepMove]);
    }
} else if (dir === 'right'){
    for(var i=0;i<lineCoords.length;i++){
    newLineCoords.push([lineCoords[i][0]+stepMove,lineCoords[i][1]]);
    }
} else if (dir === 'left'){
    for(var i=0;i<lineCoords.length;i++){
    newLineCoords.push([lineCoords[i][0]-stepMove,lineCoords[i][1]]);
    }
}
console.log("newLineCoords",newLineCoords);
var lineGeom = lineFeature.getGeometry();
var newGeom = new ol.geom.LineString(newLineCoords);
lineFeature.setGeometry( newGeom );                     
}

function rotateFeat(){
var lineGeom = lineFeature.getGeometry();
var lineExtent = lineGeom.getExtent();
var center = ol.extent.getCenter(lineExtent);
console.log("center",center);
lineGeom.applyTransform(rotateTransform(45, center[0], center[1]));
}


var getRotation = function(dx, dy) {
    var rot = 0;

    if (dx == 0 && dy == 0) return 0;

    if (dy == 0)
        rot = (dx > 0) ? 0 : Math.PI;
    else if (dx == 0)
        rot = (dy > 0) ? 0.5 * Math.PI : 1.5 * Math.PI;
    else {
        var rot = Math.atan(dy/dx);
        if (dx < 0 && dy > 0) rot += Math.PI;
        if (dx < 0 && dy < 0) rot += Math.PI;
        if (dx > 0 && dy < 0) rot += 2 * Math.PI;
    }
    return rot;
};
var rotateTransform = function (deg, x, y) {
    return function(inArr, outArr, dim) {
        for (var i = 0; i < inArr.length; i+=dim) {
            var ptx     = inArr[i];
            var pty     = inArr[i+1];
            var rot     = getRotation(ptx - x, pty - y);
            var rad     = Math.sqrt(Math.pow(ptx - x, 2) + Math.pow(pty - y, 2));
            var newRot  = rot + deg / 180 * Math.PI;
            outArr[i]   = (x + rad * Math.cos(newRot));
            outArr[i+1] = (y + rad * Math.sin(newRot));
        }
        return outArr;
    };
};

end a working fiddle here 在这里结束工作的小提琴

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

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