javascript-动态更新标记Openlayers 3的位置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-动态更新标记Openlayers 3的位置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2604字,纯文字阅读大概需要4分钟。
内容图文
![javascript-动态更新标记Openlayers 3的位置](/upload/InfoBanner/zyjiaocheng/664/5ba58d9b6960425ca6c372538633478c.jpg)
我有这段代码来显示车辆的当前位置
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函数.但是这样做不会更新位置.知道怎么做吗?
解决方法:
全局初始化您的图标,矢量源和图层
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);
我没有测试它,因为我没有时间制造小提琴.如果您确实需要具体的解决方案,则应该摆弄小提琴帮助我们,以帮助您.
内容总结
以上是互联网集市为您收集整理的javascript-动态更新标记Openlayers 3的位置全部内容,希望文章能够帮你解决javascript-动态更新标记Openlayers 3的位置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。