R
R
RuBear2020-01-28 00:22:57
API
RuBear, 2020-01-28 00:22:57

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

2 answer(s)
A
Anton Shamanov, 2020-01-28
@SilenceOfWinter

for x and y they are also lat and lng - https://on55.ru/articles/2

F
freeExec, 2020-01-28
@freeExec

Fill in the coordinates already in the code and do not use routing. For such a rough estimate, the difference between the longitude and latitude of the coordinates is enough for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question