Dark19,
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
var mapsScript = document.createElement('script');
mapsScript.onload = function() {
ymaps.ready(init);
}
mapsScript.src = 'http://api-maps.yandex.ru/2.1/?lang=ru_RU';
document.body.appendChild(mapsScript);
var myMap;
function init () {
var centroids = [
{
object: "A",
point: [50.4404, 30.5582]
},
{
object: "B",
point: [49.3905, 27.0426]
},
{
object: "C",
point: [48.4931, 32.2721]
},
{
object: "D",
point: [49.5623, 34.5573]
}
];
var points = [
{
point: [50.2153, 29.5036]
},
{
point: [50.3561, 32.1842]
},
{
point: [48.5515, 29.5915]
},
{
point: [47.6686, 32.4040]
},
{
point: [48.6972, 35.7438]
},
{
point: [47.1016, 35.2604]
}
];
var i =0;
var j = 0;
var k = 0, pointsDistance = [];
for(i;i<points.length;i++){
pointsDistance[i] = {length : 0}
for(j;j<centroids.length;j++){
(function(a, b) {
ymaps.route([points[a]['point'], centroids[b]['point']]).then(function (router) {
var distance = Math.round(router.getLength() / 1000);
pointsDistance[a][centroids[b]["object"]] = distance
pointsDistance[a]["length"]++;
if(pointsDistance[a]["length"] == centroids.length) k++;
if (k == points.length) {
$.each(pointsDistance, function(b, c) {
var d = c.A, centre = "A";
for(var k in c) {
var h = c[k];
k != "length" && h < d && (d = h, centre = k)
}
pointsDistance[b] = centre
});
alert(pointsDistance)
}
});
})(i,j)
}
j=0;
}
;
}
</script>
</body>
</html>