我使用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行)传递到顶部的两个输入中?我只需要没有文字的数字。
答案 0 :(得分:0)
document.querySelectorAll("input[name='dist']")[0].value = distance; // distance
document.querySelectorAll("input[name='price']")[0].value = self.calculate(distance) // price
答案 1 :(得分: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
});