最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Move feature from one location to another in OpenLayers 3 - Stack Overflow

programmeradmin5浏览0评论

How do I move a vector feature from one position on a map to another?

I have the following which generates an icon at (0.0, 0.0):

var iconFeature = new ol.Feature({
   geometry: new ol.geom.Point([0.0,0.0])
});

var iconStyle = new ol.style.Style({
  image: new ol.style.Icon(({
    anchor: [0.5, 46],
    anchorXUnits: 'fraction',
    anchorYUnits: 'pixels',
    opacity: 0.75,
    src: 'marker-icon.png'
  }))
});

iconFeature.setStyle(iconStyle);

This works fine, but how do I now move it to another location?

I've tried:

iconFeature.move(x,y);

I've also tried

iconFeature.geometry.move(x,y);

The latter says that iconFeature.geometry is undefined, the first says icon.move() is not a function.

Previous answers on SO suggest these solutions, but they don't seem to work for me.

How do I move a vector feature from one position on a map to another?

I have the following which generates an icon at (0.0, 0.0):

var iconFeature = new ol.Feature({
   geometry: new ol.geom.Point([0.0,0.0])
});

var iconStyle = new ol.style.Style({
  image: new ol.style.Icon(({
    anchor: [0.5, 46],
    anchorXUnits: 'fraction',
    anchorYUnits: 'pixels',
    opacity: 0.75,
    src: 'marker-icon.png'
  }))
});

iconFeature.setStyle(iconStyle);

This works fine, but how do I now move it to another location?

I've tried:

iconFeature.move(x,y);

I've also tried

iconFeature.geometry.move(x,y);

The latter says that iconFeature.geometry is undefined, the first says icon.move() is not a function.

Previous answers on SO suggest these solutions, but they don't seem to work for me.

Share Improve this question edited Dec 1, 2015 at 9:01 Christophe Roussy 17k5 gold badges92 silver badges85 bronze badges asked Oct 31, 2014 at 18:10 Single EntitySingle Entity 3,1153 gold badges42 silver badges66 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

Actually you can do this using the new ol.coordinate.add method, see the docs.

I have added a jsFiddle demonstrating this, red points are the original, and the green ones are those points moved a random distance.

To get the points, use forEachFeature on the vector source, and getGeometry().getCoordinates() to get the actual coordinates, and the setGeometry, eg,

vectorSource.forEachFeature(function(feature){
     var coordinate = feature.getGeometry().getCoordinates();
     //move coordinates some distance
     ol.coordinate.add(coordinate, 10, 10);
     //use setGeometry to move it   
     feature.setGeometry(new ol.coordinate);
    }
);

In this fiddle I created a new geometry rather than moving the existing one. Obviously, for something other than a point you would have to iterate through the coordinates array. To only move a specific geometry, there are is the getFeatureById method of ol.Feature that can be used to get a specific feature, whose geometry you can then move and update, as above.

There's translate method for moving geometries:

iconFeature.getGeometry().translate(deltaX, deltaY);

NB, that's for relative moving. If you want to move your point to absolute position, you have to calculate distances between original and desired location.

发布评论

评论列表(0)

  1. 暂无评论