Answer the question
In order to leave comments, you need to log in
How to find the last polygon through which the route passed on Yandex Maps API 2.1?
Good afternoon! The task is this. Several polygons are drawn on the map. It is necessary to lay a route from the center of the polygon to the specified address. Calculate the route length. Calculate the length of the route in each polygon and outside the polygon.
Did everything listed above. To do this, I create polygons, lay a route and break it into segments. Further using geoQuery I receive all necessary data. Everything is fine. The last one remains: I need to find the last polygon through which the route passed. How can this be done?
// второй способ поиска адреса и расчета зон
function findAddressTwo(address, zone) {
ymaps.geocode(address).then(function (r) {
var firstObject = r.geoObjects.get(0);
if (firstObject) {
//сообщаем
raiseEvent(firstObject.geometry.getCoordinates(), "coords");
// создаем метку
var placemark = new ymaps.Placemark(firstObject.geometry.getCoordinates());
routes.removeAll();
placemark.properties.set('hintContent', address);
routes.add(placemark);
// создаем выборку
var geoStorage = ymaps.geoQuery(myMap.geoObjects);
// получаем зону куда попала метка
var zones = geoStorage.searchContaining(placemark);
// расчет
geoStorage.search(`properties.zone = ${zone}`).each(function (polygon) {
// получаем точку в текущей зоне
geoStorage.searchInside(polygon).search('geometry.type = "Point"').each(function (point) {
if (!(point == placemark)) {
// построим маршрут
ymaps.route([
placemark.geometry.getCoordinates(),
point.geometry.getCoordinates()
]).then(function (route) {
var edges = getEdges(route);
var geoQuery = ymaps.geoQuery(edges).setOptions('strokeWidth', 3).addTo(routes);
// расчет полигонов
geoStorage.search('geometry.type = "Polygon"').each(function (polygon) {
var distance = route.getLength();
var distanceIn = getDistance(geoQuery.searchInside(polygon));
var distanceOut = getDistance(geoQuery.remove(geoQuery.searchInside(polygon)).remove(geoQuery.searchIntersect(polygon)));
var polygonZone = polygon.properties.get('zone');
var placemarkZone = zones.getLength() > 0 ? zones.get(0).properties.get('zone') : 9;
raiseEvent({
zoneName: polygon.properties.get('hintContent'),
distanceAll: distance,
distanceInZone: distanceIn,
distanceOutZone: distanceOut,
polygonZone: polygonZone,
placemarkZone: placemarkZone
}, 'zone');
});
});
}
});
});
}
});
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question