Emis Posted December 12, 2018 Report Share Posted December 12, 2018 Sveiki! Ir gatavs jQuery skrips, kurš no google matrix un place izvelk distanci, starp lokācijām. Tehniski skripts strādā, bet netieku skaidrībā, kadēļ šo aprēķināto distanci nevar izprintēt. Ievadot datus laukos, pārlūka konsole datus izvada pareizi, bet iekš html neprintē. Varbūt, kāds var parādīt, kā pēc šī skripta var atsevišķi izprintēt distance.value. Paldies! $(function() { // add input listeners google.maps.event.addDomListener(window, 'load', function () { var from_places = new google.maps.places.Autocomplete(document.getElementById('from_places')); var to_places = new google.maps.places.Autocomplete(document.getElementById('to_places')); google.maps.event.addListener(from_places, 'place_changed', function () { var from_place = from_places.getPlace(); var from_address = from_place.formatted_address; $('#origin').val(from_address); }); google.maps.event.addListener(to_places, 'place_changed', function () { var to_place = to_places.getPlace(); var to_address = to_place.formatted_address; $('#destination').val(to_address); }); }); // calculate distance function calculateDistance() { var origin = $('#origin').val(); var destination = $('#destination').val(); var service = new google.maps.DistanceMatrixService(); service.getDistanceMatrix( { origins: [origin], destinations: [destination], travelMode: google.maps.Polyline.boolean, //unitSystem: google.maps.UnitSystem.IMPERIAL, // miles and feet. unitSystem: google.maps.UnitSystem.metric, // kilometers and meters. avoidHighways: false, avoidTolls: false }, callback); } // get distance results function callback(response, status) { if (status != google.maps.DistanceMatrixStatus.OK) { $('#result').html(err); } else { var origin = response.originAddresses[0]; var destination = response.destinationAddresses[0]; if (response.rows[0].elements[0].status === "ZERO_RESULTS") { $('#result').html("Better get on a plane. There are no roads between " + origin + " and " + destination); } else { var distance = response.rows[0].elements[0].distance; var duration = response.rows[0].elements[0].duration; console.log(response.rows[0].elements[0].distance); var distance_in_kilo = distance.value / 1000; // the kilom var distance_in_mile = distance.value / 1609.34; // the mile var duration_text = duration.text; var duration_value = duration.value; $('#in_mile').text(distance_in_mile.toFixed(2)); $('#in_kilo').text(distance_in_kilo.toFixed(2)); $('#duration_text').text(duration_text); $('#duration_value').text(duration_value); $('#from').text(origin); $('#to').text(destination); } } } // print results on submit the form $('#distance_form').submit(function(e){ e.preventDefault(); calculateDistance(); }); }); Quote Link to comment Share on other sites More sharing options...
e-remit Posted December 12, 2018 Report Share Posted December 12, 2018 No konsoles tos datu laukos ierakstīt vari? Kas ir tie '#in_mile ' un '#in_kilo'? Kādi DIV vai SPAN tagi, vai varbūt INPUT lauki? Pēdējā gadījumā to jāaizpilda ar val(), nevis text(). Quote Link to comment Share on other sites More sharing options...
Emis Posted December 13, 2018 Author Report Share Posted December 13, 2018 (edited) 14 hours ago, e-remit said: No konsoles tos datu laukos ierakstīt vari? Kas ir tie '#in_mile ' un '#in_kilo'? Kādi DIV vai SPAN tagi, vai varbūt INPUT lauki? Pēdējā gadījumā to jāaizpilda ar val(), nevis text(). Paldies par atbildi. Es jau paspēju atrast citu, daudz vienkāršāku variantu izmantojot phpm kas pat manā situācija der labāk, kā arī strādā un viss pareizi. Bet varbūt vari padalīties ar info. Šobrīd situācija ir tāda, ka man distanci aprēķina izmantojot ceļus, šodejas utt, bet man vajadzētu lai izmēra pa taisnu līniju(pa gaisu). Vai āķis ir tajā, kādu srciptu es embedoju no google API? respektīvi, šobrīd ir https://maps.googleapis.com/maps/api/js?libraries=places&language=en&..... bet varbūt ir jāizmanto matrix API ar tam piederošo embedojamo skriptu? Paldies! P.s. screenshots no iepriekšējās sāpes. Edited December 13, 2018 by Emis Quote Link to comment Share on other sites More sharing options...
e-remit Posted December 13, 2018 Report Share Posted December 13, 2018 Dabū punktu koordinātas un tad ar formulu dabū attālumu. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.