0

I use the Yandex API to count the distance and price of a trip from point A to B. You can try yourself by clicking 2 times in different parts of a map in my example: http://jsfiddle.net/EveGeen/wor9afo0/

ymaps.route([start, finish]).then(function (router) {
    var distance = Math.round(router.getLength() / 1000),
        message = '<span>Distance: ' + distance + 'km.</span><br/>' +
            '<span style="font-weight: bold; font-style: italic">Price: %sр.</span>';

    self._route = router.getPaths();

    self._route.options.set({ strokeWidth: 5, strokeColor: '0000ffff', opacity: 0.5 });
    self._map.geoObjects.add(self._route);
    self._start.properties.set('balloonContentBody', startBalloon + message.replace('%s', self.calculate(distance)));
    self._finish.properties.set('balloonContentBody', finishBalloon + message.replace('%s', self.calculate(distance)));

});

You will see that after A, B is set you can press on any of these letters and it will show distance and price.

How can I pass these two values (distance and price, lines 127-128) into two of my inputs at the top? I need only digits without text.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
EveGeen
  • 3
  • 2

2 Answers2

0
document.querySelectorAll("input[name='dist']")[0].value = distance; // distance
document.querySelectorAll("input[name='price']")[0].value = self.calculate(distance) // price
Starmetal
  • 740
  • 6
  • 14
0

Add id to the input elements :

<label>Distance:</label>
    <input id="dist" type="text" size="40" name="dist">
    <br>
    <label>Price:</label>
    <input id="price" type="text" size="40" name="price">

and add these set their value in route event this way:

document.getElementById("dist").value = distance; // distance here
document.getElementById("price").value = self.calculate(distance);// price here

You need to set the value of input elements in route event :

ymaps.route([start, finish])
    .then(function (router) {
    var distance = Math.round(router.getLength() / 1000),
        message = '<span>Distance: ' + distance + 'km.</span><br/>' +
            '<span style="font-weight: bold; font-style: italic">Price: %sр.</span>';

    self._route = router.getPaths();

    self._route.options.set({
        strokeWidth: 5,
        strokeColor: '0000ffff',
        opacity: 0.5
    });
    self._map.geoObjects.add(self._route);
    self._start.properties.set('balloonContentBody', startBalloon + message.replace('%s', self.calculate(distance)));
    self._finish.properties.set('balloonContentBody', finishBalloon + message.replace('%s', self.calculate(distance)));

    document.getElementById("dist").value = distance; // distance here
    document.getElementById("price").value = self.calculate(distance);// price here

   });

WORKING DEMO:

UPDATED FIDDLE

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160