动态更新标记Openlayers的位置3

时间:2016-02-29 09:39:50

标签: javascript jquery openlayers-3

我有这个代码用于显示车辆的当前位置

var icon="http://www.openstreetmap.org/openlayers/img/marker.png";
window.setInterval (function () {
    $.ajax({
        url:"Dispatch_Action.vms?parameter=vehiclelive&action=customfilter",
        type:"GET",
        cache:false,
        dataType: 'json',
        success:function(response) {
             $.each(response, function(recordCount, records) {
                $.each(records, function(index, element) {

                    var createIcon=addMarker(element.LongitudePosition,element.LatitudePosition,icon); 
                });
            }); 


        }, error:function() {
            console.log("Connection Failed");
        }
    });
}, 4000);

我需要在下一次ajax调用中更新车辆的位置。我的addMarker函数如下

function addMarker(lon,lat,icon) {


var iconFeatures=[];

var iconGeometry=new ol.geom.Point(ol.proj.transform([lon,lat], 'EPSG:4326','EPSG:3857'));
var iconFeature = new ol.Feature({
    geometry:iconGeometry
});

iconFeatures.push(iconFeature);

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

var iconStyle = new ol.style.Style({
    image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
        anchor: [0.5, 46],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        opacity: 0.95,
        src:icon
    }))
});

var vectorLayer = new ol.layer.Vector({
    source: vectorSource,
    style: iconStyle
});

map.addLayer(vectorLayer);
return iconFeature;

}

当这个函数返回iconFeature时,我可以使用setCoordinate函数。但这样做不会更新位置。知道该怎么做吗?

1 个答案:

答案 0 :(得分:3)

初始化你的iconfetaures,矢量来源和图层

var iconFeatures=[];
var vectorSource = new ol.source.Vector({
    features: iconFeatures //add an array of features
});
var vectorLayer = new ol.layer.Vector({
    source: vectorSource,
    style: iconStyle
});

map.addLayer(vectorLayer);

创建一个填充标记的函数

function addMarker(lon,lat,icon) {


var iconGeometry=new ol.geom.Point(ol.proj.transform([lon,lat], 'EPSG:4326','EPSG:3857'));
var iconFeature = new ol.Feature({
    geometry:iconGeometry
});

iconFeatures.push(iconFeature);
}

您的通话代码应该是

var icon="http://www.openstreetmap.org/openlayers/img/marker.png";
window.setInterval (function () {
//clean the layer from any existing markers
vectorSource.clear();
    $.ajax({
        url:"Dispatch_Action.vms?parameter=vehiclelive&action=customfilter",
        type:"GET",
        cache:false,
        dataType: 'json',
        success:function(response) {
             $.each(response, function(recordCount, records) {
                $.each(records, function(index, element) {

                    var createIcon=addMarker(element.LongitudePosition,element.LatitudePosition,icon); 
                });
            }); 
     //and here add the newly created features to the layer
     vectorSource.addFeatures(iconFeatures);

        }, error:function() {
            console.log("Connection Failed");
        }
    });
}, 4000);

我没有测试它,因为我没有时间创造一个小提琴。如果你真的需要一个具体的解决方案,你应该做一个小提琴来帮助我们,以帮助你。