Реализован пример из документации (Проверка адреса на попадание в зону доставки), но с изменениями. В моём случае иначе поступают данные, работать нужно с тем, что есть. Собираю json feature collection, аналогичный примеру. Проблемы возникает сразу 2:
1.Не добавляются на карту сами полигоны
2.При поиске методом searchContaining вылетает ошибка "Cannot read property '0' of null"
Код на Plnkr'е
ymaps.ready(init);
function init() {
var myMap = new ymaps.Map('map', {
center: [30.264981955459618, 59.9567962610097],
zoom: 10,
controls: ['zoomControl']
});
var myPlacemark = new ymaps.Placemark([60.014596, 30.393529], {}, {
preset: 'islands#redCircleIcon'
});
myMap.geoObjects.add(myPlacemark)
var xhr = new XMLHttpRequest();
xhr.open('GET', 'data.json', true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (xhr.status != 200) {
alert('ошибка: ' + (xhr.status ? xhr.statusText : 'запрос не удался'));
return;
}
xhrResult = xhr.responseText;
var routesData = JSON.parse(xhrResult)
var jsonData = {
'type': "FeatureCollection",
'features': []
};
for (var newPolygon of routesData) {
var coords = JSON.parse(newPolygon.MapArea);
if (coords && newPolygon.Id > 0) {
jsonData.features.push({
"type": "Feature",
"id": newPolygon.Id,
"geometry": {
"type": "Polygon",
"coordinates": coords
},
"options": {
"strokeColor": "#FFFFFF",
"fillColor": newPolygon.Color,
"fillOpacity": 0.4
},
"properties": {
"name": newPolygon.Name + " " + newPolygon.Description
}
})
}
}
var deliveryZones = ymaps.geoQuery(jsonData).addToMap(myMap);
var targetZone = deliveryZones.searchContaining(myPlacemark)
console.log(targetZone);
}
}