0

我使用 Yandex API 计算从 A 点到 B 点的行程距离和价格。在我的示例中,您可以通过在地图的不同部分单击 2 次来尝试自己: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)));

});

你会看到在A、B设置好之后,你可以按这些字母中的任何一个,它会显示距离和价格。

如何将这两个值(距离和价格,第 127-128 行)传递到顶部的两个输入中?我只需要没有文字的数字。

4

2 回答 2

0
document.querySelectorAll("input[name='dist']")[0].value = distance; // distance
document.querySelectorAll("input[name='price']")[0].value = self.calculate(distance) // price
于 2014-09-22T17:04:44.933 回答
0

将 id 添加到输入元素:

<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">

route并以这种方式添加这些设置它们的值:

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

您需要在 route事件中设置输入元素的值:

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

   });

工作演示:

更新的小提琴

于 2014-09-22T17:04:51.977 回答