最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Marker or overlay moving animation moving smoothly in openlayers 3 - Stack Overflow

matteradmin10PV0评论

I created a plunkr which has a moving marker like a car.

var olview = new ol.View({
    center: [-5484111.13, -1884437.22],
    zoom: 18,
    minZoom: 2,
    maxZoom: 20
});

var osm = new ol.source.OSM();

var lineString  = new ol.geom.LineString([]);

var map = new ol.Map({
    target: 'map',
    view: olview,
    renderer: 'canvas',
    layers: [
        new ol.layer.Tile({ 
            source: osm,
            opacity: 0.6
        })
    ]
});

var car  = document.getElementById('geo1');

var marker = new ol.Overlay({
    positioning: 'center-center',
    offset: [0, 0],
    element: car,
    stopEvent: false
});

map.addOverlay(marker);


var path = [
    [-5484101.57, -1884475.44],
    [-5484114.71, -1884432.74],
    [-5484117.70, -1884416.62],
    [-5484106.95, -1884392.28]
];

lineString.setCoordinates(path);

map.once('postpose', function(event) {
    console.info('postpose');
    interval = setInterval(animation, 500);
});

var i = 0, interval;
var animation = function(){

    if (i == path.length){
        i = 0;
    }

    marker.setPosition(path[i]);
    i++;
};

This is written in openlayers, I want it to look smooth when moving similar to this.

I'm a plete beginner in openlayers, can someone help me with this? Thanks!

I created a plunkr which has a moving marker like a car.

var olview = new ol.View({
    center: [-5484111.13, -1884437.22],
    zoom: 18,
    minZoom: 2,
    maxZoom: 20
});

var osm = new ol.source.OSM();

var lineString  = new ol.geom.LineString([]);

var map = new ol.Map({
    target: 'map',
    view: olview,
    renderer: 'canvas',
    layers: [
        new ol.layer.Tile({ 
            source: osm,
            opacity: 0.6
        })
    ]
});

var car  = document.getElementById('geo1');

var marker = new ol.Overlay({
    positioning: 'center-center',
    offset: [0, 0],
    element: car,
    stopEvent: false
});

map.addOverlay(marker);


var path = [
    [-5484101.57, -1884475.44],
    [-5484114.71, -1884432.74],
    [-5484117.70, -1884416.62],
    [-5484106.95, -1884392.28]
];

lineString.setCoordinates(path);

map.once('postpose', function(event) {
    console.info('postpose');
    interval = setInterval(animation, 500);
});

var i = 0, interval;
var animation = function(){

    if (i == path.length){
        i = 0;
    }

    marker.setPosition(path[i]);
    i++;
};

This is written in openlayers, I want it to look smooth when moving similar to this.

https://github./terikon/marker-animate-unobtrusive

I'm a plete beginner in openlayers, can someone help me with this? Thanks!

Share Improve this question asked Jun 11, 2015 at 9:18 The BassmanThe Bassman 2,3615 gold badges30 silver badges41 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I've made three tests. First and second are pure ol3 and last is with tween.js.

I'm using Arc.js to create a path.

The first example is using setInterval.

The second example is using window.requestAnimationFrame.

And the last with Tween.js.

Your example doesn't run smoothly because it is just a few coordinates.

Note that the Tween.js integration is not an integration at all. It is just a tricky CSS manipulation.

Post a comment

comment list (0)

  1. No comments so far