在OpenLayers 3中动态地添加和删除轨道段 [英] Dynamically adding and removing segments from a track in OpenLayers 3

查看:1423
本文介绍了在OpenLayers 3中动态地添加和删除轨道段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用OpenLayers 3实时显示一条轨迹,最后如同蜗牛轨迹一样分散。



只需将新坐标附加到LineString就很容易。请参阅此示例。但是从行尾删除坐标看起来没有被API支持。



我应该怎么做?扩展LineString类是唯一的选择吗?或者应该为每个细分受众群使用单独的功能?



更新

我用这个代码与ol-debug.js。但get / setFlatCoordinates不会在编译版本中导出。

  var flatCoordinates = geometry.getFlatCoordinates(); (flatCoordinates&& flatCoordinates.length> 100){
//从数组$ b中移除第一个坐标元素$ b $ flatcoordinates.splice(0,geometry.getStride() );
//以坐标元素作为参数调用推送
Array.prototype.push.apply(flatCoordinates,coordinate);
//更新坐标调用change()
geometry.setFlatCoordinates(geometry.getLayout(),flatCoordinates);
}
else {
geometry.appendCoordinate(coordinate);


解决方案

appendCoordinate 方法是添加一个相当常见的用例的快捷方式坐标到LineString的末尾。要修改的几何形状与更多的控制权,与 setCoordinates设置你想要的坐标

  var maxCoords = 100; 
var coords = lineString.getCoordinates(); //获得坐标数组
coords.unshift(newCoord); //添加到数组的开头
if(coords.length> maxCoords){
coords.length = maxCoords;
}
lineString.setCoordinates(coords);


I want to display a track in real-time with OpenLayers 3 which disolves at the end, just like a snails trail.

Only appending new coordinates to a LineString is easy. See this example. But removing coordinates from the end of the line does not seem to be supported by the API.

How should I go about that? Is extending the LineString class the only option? Or should I use a separate feature for each line segment?

Update:

I used this code with ol-debug.js. But get/setFlatCoordinates is not exported in the compiled version.

var flatCoordinates = geometry.getFlatCoordinates();  // not exported
if (flatCoordinates && flatCoordinates.length > 100) {
  // remove first coordinate elements from array
  flatCoordinates.splice(0, geometry.getStride());
  // call push with coordinate elements as arguments
  Array.prototype.push.apply(flatCoordinates, coordinate);
  // update coordinates calling change()
  geometry.setFlatCoordinates(geometry.getLayout(), flatCoordinates);
} 
else {
  geometry.appendCoordinate(coordinate);
}

解决方案

The appendCoordinate method is a shortcut for the fairly common use case of adding a coordinate to the end of a LineString. To modify geometries with more control, set your desired coordinates with setCoordinates.

var maxCoords = 100;
var coords = lineString.getCoordinates(); // get coordinate array
coords.unshift(newCoord); // add to beginning of array
if (coords.length > maxCoords) {
    coords.length = maxCoords;
}
lineString.setCoordinates(coords);

这篇关于在OpenLayers 3中动态地添加和删除轨道段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆