0

Вывожу круги с координатами городов по названиям через геокодирование. Все работает. Вывожу круги с координатами из массива json. Выводит в Африке странную сетку.

<div id="map" style="width: 100%; height: 400px;"></div>
<script src="https://api-maps.yandex.ru/2.1/?lang=ru_RU&apikey=xxxxxxxxxxxxx" type="text/javascript"></script>
<script type="text/javascript">
    function declOfNum(n, text_forms) {  
    n = Math.abs(n) % 100; 
    var n1 = n % 10;
    if (n > 10 && n < 20) { return text_forms[2]; }
    if (n1 > 1 && n1 < 5) { return text_forms[1]; }
    if (n1 == 1) { return text_forms[0]; }
    return text_forms[2];
}
var data = [
        {
            "city": "Москва",
            "choord": "55.755864, 37.617698",
            "qty": "1632"
        },
        {
            "city": "Екатеринбург",
            "choord": "56.838011, 60.597474",
            "qty": "160"
        },
        {
            "city": "Санкт-Петербург",
            "choord": "59.938955, 30.315644",
            "qty": "1155"
        }, {
            "city": "Белово",
            "choord": "54.42212,86.303701",
            "qty": "2"
        }
    ];var myMap;
ymaps.ready(init);        
function init() {
    myMap = new ymaps.Map("map", {
        center: [55.76, 37.64],
        zoom: 5
    }, {
        searchControlProvider: 'yandex#search'
    });
var table = document.createElement('table'), tr, td, row, cell;
    data.forEach(function(row) {
        
        
        
        
        
        ymaps.geocode(row.city).then(function (res) {
            var coord = res.geoObjects.get(0).geometry.getCoordinates();
            
            
             tr = document.createElement('tr');
    
        td = document.createElement('td');
        tr.appendChild(td);
        td.innerHTML = row.city;
            td2 = document.createElement('td');
        tr.appendChild(td2);
        td2.innerHTML =  coord;
            td3 = document.createElement('td');
        tr.appendChild(td3);
        td3.innerHTML =  row.qty;
     
    table.appendChild(tr);
            
            
            
         
            
                var myCircle = new ymaps.Circle([
        // Координаты центра круга.
        row.choord,
        // Радиус круга в метрах.
        200*row.qty
    ], {
         
        // Содержимое хинта.
        hintContent: coord + " " + row.city + " " + row.choord
    }, {
         
        // Последний байт (77) определяет прозрачность.
        // Прозрачность заливки также можно задать используя опцию "fillOpacity".
        fillColor: "#99006677",
        // Цвет обводки.
        strokeColor: "#990066",
        // Прозрачность обводки.
        strokeOpacity: 0.8,
        // Ширина обводки в пикселях.
        strokeWidth: 5
    });

    // Добавляем круг на карту.
    myMap.geoObjects.add(myCircle);
            
                
            setCenter();
        }, function (err) {
            setCenter();
        });     
    });
    
    document.getElementById('tabl').appendChild(table);
}
 
var placemark_index = 0;
function setCenter(){
    placemark_index += 1;
    if (data.length == placemark_index) {
        myMap.setBounds(myMap.geoObjects.getBounds(),{checkZoomRange:true, zoomMargin:9});
    }
}
 
</script>
    <div id="tabl"></div>

Json взят из таблички с координатами, которые выдаёт api Карт.

1 ответ 1

1

Координаты в API Яндекс Карт это не строки, а массивы должны быть. Замените кавычки на скобки []:

{
 "city": "Москва",
 "choord": [55.755864, 37.617698],
 "qty": "1632"
}

и карта заработает как надо: https://jsbin.com/xosenew/edit?js,output

1

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.