JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//api-maps.yandex.ru/2.0-stable/?load=package.standard,package.route&lang=ru-RU" type="text/javascript"></script>
<script type="text/javascript">// <![CDATA[
function init() {
var myMap = new ymaps.Map('map', {
center: [55.629999, 51.799940],
zoom: 10,
type: 'yandex#map',
behaviors: ['scrollZoom', 'drag']
}),
search = new ymaps.control.SearchControl({
useMapBounds: true,
noCentering: true,
noPlacemark: true
}),
calculator = new DeliveryCalculator(myMap, myMap.getCenter());
myMap.controls.add(search, { right: 5, top: 5 });
search.events.add('resultselect', function (e) {
var results = search.getResultsArray(),
selected = e.get('resultIndex'),
point = results[selected].geometry.getCoordinates();
calculator.setStartPoint(point);
});
}
function DeliveryCalculator(map, finish) {
this._map = map;
this._start = null;
this._finish = new ymaps.Placemark(finish, { iconContent: 'Б' });
this._route = null;
map.events.add('click', this._onStartPointChange, this);
map.geoObjects.add(this._finish);
}
var ptp = DeliveryCalculator.prototype;
ptp._onStartPointChange = function (e) {
this.setStartPoint(e.get('coordPosition'));
};
ptp.getDirections = function () {
var self = this,
start = this._start.geometry.getCoordinates(),
finish = this._finish.geometry.getCoordinates();
if(this._route) {
this._map.geoObjects.remove(this._route);
}
ymaps.geocode(start, { results: 1 })
.then(function (geocode) {
var address = geocode.geoObjects.get(0) &&
geocode.geoObjects.get(0).properties.get('balloonContentBody') || '';
ymaps.route([start, finish])
.then(function (router) {
var distance = Math.round(router.getLength() / 1000),
message...