-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (29 loc) · 1.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const options = {
componentRestrictions: { country: "br" },
};
const inputOrigem = document.querySelector("input.origem");
new google.maps.places.Autocomplete(inputOrigem, options);
const inputDestino = document.querySelector("input.destino");
new google.maps.places.Autocomplete(inputDestino, options);
const resultado = document.querySelector("span.valor");
const divResultadoOK = document.querySelector("div.resultado-ok");
const divResultadoERRO = document.querySelector("div.resultado-erro");
function calculaValor() {
const request = {
origin: inputOrigem.value,
destination: inputDestino.value,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC
};
new google.maps.DirectionsService().route(request, function(result, status) {
if (status === 'OK') {
const qntKm = result.routes[0].legs[0].distance.value / 1000;
const precoPorKm = 5;
const valorFinal = qntKm * precoPorKm;
resultado.innerHTML = "R$ " + valorFinal.toFixed(2).replace(".", ",");
divResultadoOK.style.display = "block";
} else {
divResultadoERRO.style.display = "block";
}
});
}