绘制动画openlayers线串路径 [英] Drawing animated openlayers linestring path

查看:751
本文介绍了绘制动画openlayers线串路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have seen an impressive mapping example on http://jerusalem.com/map#!/tour/the_way_of_the_cross/location/abu_jaafar, Does anybody how a similar animation on the drawn path of the points can done using openlayers?

以下小提琴显示了线串 http://jsfiddle.net/pwuVz/58/,但是我需要的是能够对线串本身进行动画处理,以使线串不会直接绘制.

The following fiddle shows the linestrings http://jsfiddle.net/pwuVz/58/ but I need is to be able to animate the line string itself so that the string is not directly drawn.

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);

推荐答案

您可以通过一次仅绘制线条的一部分来对其进行动画处理.这是您可以执行的一种方法:

You can animate it by drawing only one part of the line at a time. Here is one way you could do it:

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是动画制作完成后将执行的回调.

The time argument specifies how long you want the animation to last (in milliseconds), and the steps specifies how many steps you want to divide the animation into. fn is a callback that will be executed when the animation is complete.

这是一个 jsFiddle演示进行演示.

这篇关于绘制动画openlayers线串路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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