-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
96 lines (79 loc) · 3.09 KB
/
javascript.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const optionsMap = {
center: {
lat: -5.79448,
lng: -35.211
},
zoom: 13,
types :'cities',
radius :'100'
// mapTypeId: 'roadmap',//ALTERA MODO VISUALIZAÇÃO DO MAPA || roadmap, satellite, hybrid, terrain
};
const from = document.getElementById("from");
const to = document.getElementById("to");
const map = new google.maps.Map(
document.getElementById('googleMap'),
optionsMap
);
const directionsService = new google.maps.DirectionsService();
const directionsDisplay = new google.maps.DirectionsRenderer();
const autoCompleteFrom = new google.maps.places.Autocomplete(
from,
optionsMap.types,
optionsMap.radius
);
const autoCompleteTo = new google.maps.places.Autocomplete(
to,
optionsMap.types,
optionsMap.radius
);
directionsDisplay.setMap(map);
function setInfoRoute(){
//CODIGO PARA PODE ESCOLHER O TIPO DE VIAGEM DRIVING WALKING, BYCYCLING, TRANSIT
//const selectedMode = document.getElementById("mode").value;
const optionsRoute = {
origin: document.getElementById('from').value,
destination: document.getElementById('to').value,
travelMode: google.maps.TravelMode.DRIVING, //[selectedMode], //WALKING, BYCYCLING, TRANSIT
unitSystem: google.maps.UnitSystem.METRIC, //IMPERIAL
provideRouteAlternatives: true //DISPONIBILIZA MAIS DE UMA ROTA
}
return optionsRoute
}
function getInfoMap(result){
const response ={
form: document.getElementById("from").value, //INPUT DO HTML
to: document.getElementById("to").value, //INPUT DO HTML
distanceText: result.routes[0].legs[0].distance.text,
durationsText: result.routes[0].legs[0].duration.text,
lngFrom: result.routes[0].legs[0].start_location.lng(),//LONGITUDE ORIGEM
latFrom: result.routes[0].legs[0].start_location.lat(),//LATITUDE ORIGEM
lngTo: result.routes[0].legs[0].end_location.lng(),//LONGITUE DESTINO
latTo: result.routes[0].legs[0].end_location.lat()//LATITUDE DESTINO
//distanceValue: result.routes[0].legs[0].distance.value, DISTANCIA EM METROS
//durationsValue: result.routes[0].legs[0].duration.value, TEMPO EM SEGUNDOS
}
return response
}
function calcRoute(){
infoRoute = setInfoRoute()
function displayResult(result,status){
if(status === "OK"){
infoMap = getInfoMap(result)
$("#output").html(
"<div class='alert-info'>"+
"From: "+infoMap.form+
"<br/>To: "+infoMap.to+
"<br/>Driving Distance: "+infoMap.distanceText+
"<br/>Durations: "+infoMap.durationsText+
"</div>"
);
directionsDisplay.setDirections(result);
}
else {
$("#output").html(
"<div class='alert-danger'>Could not retrive distance</div>"
);
}
}
directionsService.route(infoRoute, displayResult);
}