R
R
Raido2014-05-22 18:24:33
JavaScript
Raido, 2014-05-22 18:24:33

Yandex.maps - why are objects received from json not displayed?

Good day! Tell me what I'm doing wrong .. I can't display objects on the map.
all code

ymaps.ready(init);

function init() {


    var myMap = new ymaps.Map("map", {
            center: [53.187814, 50.113989],
            zoom: 1
    });

    var coords = [];
    var names = [];

$.getJSON(
        "https://dl.dropboxusercontent.com/u/18781731/geoadv.json",
            function(data) {
            for (var key in data) {
                coords.push(data[key].fields.geo);
                names.push(data[key].fields.name);
            }

    var myCollection = new ymaps.GeoObjectCollection({}, {preset: 'twirl#shopIcon'});
        for (var i = 0, l = coords.length; i < l; i++) {
            myCollection.add(new ymaps.Placemark(parseFloat(coords[i]),
            {
                balloonContentHeader:names[i]
            }));
            console.log(coords[i]);
        }
        console.log(coords);
        myMap.geoObjects.add(myCollection);
        //myMap.setBounds(myCollection.getBounds());
    }
);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2014-05-22
@Raido

Incorrectly transmitting data about coordinates:
You need to parse JSON data:

var _coords = JSON.parse(coords[i]);
myCollection.add(new ymaps.Placemark([parseFloat(_coords[0]),parseFloat(_coords[1])],
...
// Либо, если не нужна проверка на float 
myCollection.add(new ymaps.Placemark(JSON.parse(coords[i]),
...

here fork

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question