I
I
Ilia Valchenko2015-04-18 14:18:44
Yandex
Ilia Valchenko, 2015-04-18 14:18:44

How to gradually display coordinates on Yandex Map?

Good day to all! How can I gradually, after a certain period of time, display the coordinates stored in the array on Yandex Map? I understand that this is a somewhat artificial situation, but still. The first thing that came to my mind was to use setTimeout and setInterval before calling the draw point function.

// Функция ready вызовется тогда, когда API будет загружен и DOM сформирован
ymaps.ready(init);

var myMap, myPlacemark;

// Массивы координат
var Latitude = [53.8912, 53.8916, 53.8923, 53.8928,]
var Longitude = [27.5669, 27.5669, 27.5663, 27.5662,]

function init(){ 
    myMap = new ymaps.Map("map", {
        center: [Latitude[0], Longitude[0]],
        zoom: 10
    });

    for(i = 0; i < Latitude.length; i++) {
        setTimeout(addPoint, 5000, myMap, Latitude[i], Longitude[i]); 
    }
}

function addPoint(Map, lat, lon) {
    myPlacemark = new ymaps.Placemark([lat, lon, { 
        content: 'Current location', 
        balloonContent: 'myBalloonContent' 
    });

    Map.geoObjects.add(myPlacemark);
}

But I have only one single coordinate displayed. Please tell me how to make it so that every five seconds a new point is added to the map. Can I make it so that when adding a new point, only the container with the map is updated, and not the entire page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
forgotten, 2015-04-20
@forgotten

You are making the standard mistake of a person who is not familiar with JavaScript. Your code will add a label 4 times with the same coordinates.
javascript.ru/book/definitiveguide section "Scope of functions and closures"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question