创建并添加到地图后,如何更改地图上的旋转点(要素)

时间:2015-04-15 09:37:21

标签: openlayers-3

如何在创建和添加到地图后更改旋转图标(图片),点(功能)?

设置旋转图标创建一个我知道的点,但是如何在以后更改旋转? 对于跟随旋转方向的图标之类的用途?

如何在性能方面正确执行此操作? (再次设定所有参数???)

非常感谢

现场演示: http://jsfiddle.net/91mLh1j7/

JS代码:

var map = new ol.Map({
    target: 'mapID',
    layers: [
    new ol.layer.Tile({
        source: new ol.source.MapQuest({
            layer: 'osm'
        })
    })],
    view: new ol.View({
        center: ol.proj.transform([14, 50], 'EPSG:4326', 'EPSG:3857'),
        zoom: 11
    })
});



var Features = [];
// define style for features
var iconStyle = {
    src: "http://google-maps-icons.googlecode.com/files/library-publ.png",
    anchorOrigin: "bottom-left",  // v KML je počítáno od levého spodního rohu
    anchor: [0.5, 0],
    anchorXUnits: "fraction",
    anchorYUnits: "fraction",
    scale: 0.9,
    opacity: 0.75,
    rotation: 45 * 0.01745329251,  // in rad / 360° = 6.28318531 rad = 2PI rad
    rotateWithView: "true"
}; 


var point1 = new ol.Feature({
    geometry: new ol.geom.Point(ol.proj.transform([14.01, 50.01], 'EPSG:4326',
        'EPSG:3857')),
    name: 'Point One'
});
point1.setStyle(new ol.style.Style({
    image: new ol.style.Icon(iconStyle)
}));


var point2 = new ol.Feature({
    geometry: new ol.geom.Point(ol.proj.transform([13.99, 49.99], 'EPSG:4326',
        'EPSG:3857')),
    name: 'Point Two'
});
point2.setStyle(new ol.style.Style({
    image: new ol.style.Icon(iconStyle)
}));

// add point1, point2 to Features 
Features.push(point1);
Features.push(point2);

var vectorSource = new ol.source.Vector({
    features: Features  // add an array of features
});
var vectorLayer = new ol.layer.Vector({
    source: vectorSource  // add source for vectorLayer
});

map.addLayer(vectorLayer)  // add vectorLayer to map


////////////////////
// how to change the rotation of one point (feature) ? after cration point and add it on map
////////////////////

1 个答案:

答案 0 :(得分:2)

ol.style.Image扩展的ol.style.Icon类具有setRotation方法,可用于设置图标的旋转。您可以在示例中尝试添加:

Feature1.getStyle().getImage().setRotation(135 * 0.01745329251);

在更新的小提琴上观看直播:http://jsfiddle.net/91mLh1j7/1/