动态更新标记开孔层的位置 3.
Dynamically Update Position of Marker 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 调用中更新车辆的位置。我的添加标记函数如下
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;
}
由于此函数返回图标特征,我可以使用 set坐标函数。但这样做不会更新位置。知道怎么做吗??
全局初始化您的图标、矢量源和图层
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);
我没有测试它,因为我没有时间制作小提琴。如果你真的需要一个具体的解决方案,你应该做一个小提琴来帮助我们,以帮助你。
相关文章:
- 如何更改bigquery API中的计费层选项
- jquery试图按名称获取按钮位置
- 我可以获得相对于被点击元素的确切点击位置吗
- 谷歌地图固定位置覆盖
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 循环比赛位置算法
- es6 相当于下划线查找位置
- jQuery循环在特定位置暂停
- 引导程序:在导航栏中,显示悬停在单个位置的基于Li Link的不同内容
- 自定义函数中的光标位置
- 安卓平台上的QWebView HTML5地理位置
- DIV并排,位置不正确
- Javascript - 确定用户当前位置是否在谷歌地图的 KML 层中
- 动态更新标记开孔层的位置 3.
- JavaScript 或 JQuery:获取位置上最内层的元素
- 计算屏幕上特定位置存在多少层
- 谷歌地图API:希望地理位置自动点击下面的数据层
- 如何在KineticJS中获取点击事件相对于其层的位置
- 如何在可拖动层顶部单击时获得准确的指针位置
- 指向KML层上位置的HTML链接