Answer the question
In order to leave comments, you need to log in
Is it possible to select the nearest city to the user from the list in the Yandex.Maps API?
I can't find a solution on how to correctly select the nearest city to the user from a certain list.
For example, there are 3 cities for delivery of goods: Moscow, Rostov-on-Don, Sochi, the user himself, let's say, is closer to Rostov. Is it possible to determine the nearest city from the list through the API?
At the moment I use a banal solution: I request the position of the client and request the distance to cities in a loop, write everything into an array and then sort it in ascending order.
In general, the method works, but unfortunately there is a big delay when using the loop and sorting, for some reason, works every other time, and you have to output with a delay, otherwise the script does not have time to work out.
let cities = {
city1:{
name: "St. Petersburg",
feet: 0
},
city2:{
name: "Moscow",
feet: 0
},
city3:{
name: "Rostov-on-Don",
feet: 0
},
city4:{
name: "Sochi",
feet: 0
}
}
let dist = [ ];
ymaps.ready(function(){
ymaps.geolocation.get({ autoReverseGeocode: true }).then(function (result) {
let startPoint = result.geoObjects.get(0).properties.get('description').split (re).pop();
$.each(cities,function(i,e){
endPoint = e.name;
ymaps.route([startPoint, endPoint]).then(function(route){
distance = route.getHumanLength ();
dist[e.name] = parseInt(distance.replace("km",""),10);
});
});
});
});
console log(dist);
API Version - 2.1
Answer the question
In order to leave comments, you need to log in
for x and y they are also lat and lng - https://on55.ru/articles/2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question