There is a script that calculates the cost of delivery depending on the distance. Also, it calculates delivery within the Moscow Ring Road or not.
But if several times to recount, the page freezes and knocks.
PS for the code do not swear.
ymaps.ready(init); var myMap; function init() { var suggestView = new ymaps.SuggestView('from'); var suggestView2 = new ymaps.SuggestView('to'); myMap = new ymaps.Map("map", { center: [55.73, 37.75], zoom: 9 }, { searchControlProvider: 'yandex#search' }) } //$('input[value="tomorrow"]').click(); function yandex(){ var from_elem = $('#from'), to_elem = $('#to'), km_elem = $('#km'), srok_elem = $('.header-content-form-urgency input:checked'), mkad_elem = $('#mkad'), result_elem = $('.form-price span'), weight_elem = $('#weight'), result; if(from_elem.val().length > 0 && to_elem.val().length > 0) { ymaps.geocode(from_elem.val()) .then(function (res) { var cord_1 = res.geoObjects.get(0) .geometry.getCoordinates(); ymaps.geocode(to_elem.val()) .then(function (res) { var cord_2 = res.geoObjects.get(0) .geometry.getCoordinates(); /*f($('#map ymaps').length > 0) { myMap.destroy(); }*/ function onPolygonLoad (json) { moscowPolygon = new ymaps.Polygon(json.coordinates); moscowPolygon.options.set('visible', false); myMap.geoObjects.add(moscowPolygon); ymaps.route([[51.40060238976057,10.454355499999894], cord_2]).then( function (res) { var pathsObjects = ymaps.geoQuery(res.getPaths()), edges = []; pathsObjects.each(function (path) { var coordinates = path.geometry.getCoordinates(); for (var i = 1, l = coordinates.length; i < l; i++) { edges.push({ type: 'LineString', coordinates: [coordinates[i], coordinates[i - 1]] }); } }); var routeObjects = ymaps.geoQuery(edges) .add(res.getWayPoints()) .add(res.getViaPoints()) .setOptions('strokeWidth', 3) .addToMap(myMap), objectsInMoscow = routeObjects.searchInside(moscowPolygon), boundaryObjects = routeObjects.searchIntersect(moscowPolygon); console.log(objectsInMoscow); if(objectsInMoscow._objects.length > 0) { $('#mkad').val('true'); } else { $('#mkad').val('false'); } } ); } function result(){ if(mkad_elem.val()=="true") { result = (km_elem.val()*srok_elem.attr('data-mkad'))+parseInt(weight_elem.val()); $('body .header-content-form-urgency > div:first-child').addClass('disabled'); } else { result = (km_elem.val()*srok_elem.attr('data-nomkad'))+parseInt(weight_elem.val()); $('body .header-content-form-urgency > div:first-child').removeClass('disabled'); } result_elem.html(result); } setTimeout(result, 1200); $.ajax({ url: 'js/moscow.json', dataType: 'json', success: onPolygonLoad }); // Расстояние. var result = ymaps.formatter.distance(ymaps.coordSystem.geo .getDistance(cord_1, cord_2)).split('&'); km_elem.val(result[0]); }); }); $('.header-content-form').addClass('load'); $('.form-loading').addClass('form-loading-open'); function load_close(){ $('.header-content-form').removeClass('load'); $('.form-loading').removeClass('form-loading-open'); } setTimeout(load_close, 2000); } } // function yandex $('.send-btn').click(function(){ var name = $('#fio').val(), phone = $('#phone').val(), from = $('#from').val(), to = $('#to').val(), srok = $('body .header-content-form-urgency input:checked').parent().find('span:last-of-type').text(), weight = $('#weight option:selected').text(), km = $('#km').val(), price = $('.form-price span').text(), agree = $('#agree'), alerts = ''; if(name.length == 0) { alerts += "Заполните имя!\n"; } if(phone.length == 0) { alerts += "Заполните телефон!\n"; } if(!agree.is(':checked')) { alerts += "Вы должны согласиться с правилами!"; } if(name.length == 0 || phone.length == 0 || !agree.is(':checked')) { alert(alerts); return; } $.ajax({ type: 'POST', url: '../send.php', data: 'name='+name+'&phone='+phone+'&from='+from+'&to='+to+'&srok='+srok+'&weight='+weight+'&km='+km+'&price='+price }).done(function(){ alert('Спасибо, Ваша заявка успешно отправлена!'); }) }) $('.header-content-form input[name="from"], .header-content-form input[name="to"], select[name="weight"], .header-content-form input[name="urgency"]').on('change', function(){ setTimeout(yandex, 300); })