gpt4 book ai didi

javascript - 绘制动画 openlayers 线串路径

转载 作者:行者123 更新时间:2023-11-29 16:13:59 24 4
gpt4 key购买 nike

我在 http://jerusalem.com/map#!/tour/the_way_of_the_cross/location/abu_jaafar 上看到了一个令人印象深刻的映射示例, 有人知道如何使用 openlayers 在点的绘制路径上制作类似的动画吗?

下面的 fiddle 显示了线串 http://jsfiddle.net/pwuVz/58/但我需要的是能够为线条本身设置动画,以便不直接绘制字符串。

var map = new OpenLayers.Map( 'map', {theme:null,
controls:[new OpenLayers.Control.Navigation()]} );
layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0",
{layers: 'basic'} );
map.addLayer(layer);
map.setCenter([3, 49], 5);

var startPt=new OpenLayers.Geometry.Point( 2, 45);
var endPt=new OpenLayers.Geometry.Point(7,55);

//make the line:
var line=new OpenLayers.Geometry.LineString([startPt, endPt]);

//style
var style={strokeColor:"#0500bd", strokeWidth:15, strokeOpacity: 0.5, strokeColor: '#0000ff'};
//make vector
var fea=new OpenLayers.Feature.Vector(line, {}, style);

//make vectorLayer
var vec= new OpenLayers.Layer.Vector();

//add the feature
vec.addFeatures([fea]);

//add to map
map.addLayer(vec);

setTimeout(function() {

var startPt=new OpenLayers.Geometry.Point( 7, 55);
var endPt=new OpenLayers.Geometry.Point(13,52);

//make the line:
var line=new OpenLayers.Geometry.LineString([startPt, endPt]);

//style
var style={strokeColor:"#0500bd", strokeWidth:15, strokeOpacity: 0.5, strokeColor: '#0000ff'};
//make vector
var fea=new OpenLayers.Feature.Vector(line, {}, style);

//make vectorLayer
var vec= new OpenLayers.Layer.Vector();

//add the feature
vec.addFeatures([fea]);

//add to map
map.addLayer(vec);

}, 2000);

最佳答案

您可以通过一次只绘制线条的一部分来为其设置动画。这是您可以做到的一种方法:

function drawAnimatedLine(startPt, endPt, style, steps, time, fn) {
var directionX = (endPt.x - startPt.x) / steps;
var directionY = (endPt.y - startPt.y) / steps;
var i = 0;
var prevLayer;
var ivlDraw = setInterval(function () {
if (i > steps) {
clearInterval(ivlDraw);
if (fn) fn();
return;
}
var newEndPt = new OpenLayers.Geometry.Point(startPt.x + i * directionX, startPt.y + i * directionY);
var line = new OpenLayers.Geometry.LineString([startPt, newEndPt]);
var fea = new OpenLayers.Feature.Vector(line, {}, style);
var vec = new OpenLayers.Layer.Vector();
vec.addFeatures([fea]);
map.addLayer(vec);
if(prevLayer) map.removeLayer(prevLayer);
prevLayer = vec;
i++;
}, time / steps);
}

time 参数指定您希望动画持续多长时间(以毫秒为单位),steps 指定您希望将动画分成多少步。 fn 是将在动画完成时执行的回调。

这是一个jsFiddle demo这证明了这一点。

关于javascript - 绘制动画 openlayers 线串路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20410175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com