-
Notifications
You must be signed in to change notification settings - Fork 1
/
js.js
74 lines (67 loc) · 2.58 KB
/
js.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
var lat;
var lon;
var new_end;
function geolocation() {
if (!navigator.geolocation)
return null;
navigator.geolocation.getCurrentPosition((pos) => {
latUser = document.getElementById("lat").innerText = pos.coords.latitude;
lonUser = document.getElementById("lon").innerText = pos.coords.longitude;
initMap();
})
}
const image = "./person.png";
const pilon = "./pilon.png";
function initMap() {
const uluru = { lat: latUser, lng: lonUser };
map = new google.maps.Map(document.getElementById('map'), {
center: uluru,
zoom: 7,
});
const marker = new google.maps.Marker({
position: uluru,
map: map,
icon: image,
});
const final = new google.maps.Marker({
position: { lat: -23.192075, lng: -45.88987 },
map: map,
icon: pilon,
});
//Distance Matrix para calcular a distância e o tempo entre dois pontos
var origin1 = new google.maps.LatLng(latUser, lonUser);
var destinationA = new google.maps.LatLng(-23.192075, -45.88987);
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin1],
destinations: [destinationA],
travelMode: 'DRIVING',
}, callback);
function callback(response, status) {
var retorno = JSON.stringify(response);
ret = document.getElementById("retorno").innerText = retorno;
new_end = response.originAddresses[0];
var endUser = document.getElementById("userEndereco").innerText = new_end;
//ealert(response.rows.elements.distance.text);
//var tempo = document.getElementById("tempo").innerText = response.rows.elements[0];
var directionsService = new google.maps.DirectionsService();
var directionsRenderer = new google.maps.DirectionsRenderer({ suppressMarkers: true });
directionsRenderer.setMap(map);
calculateAndDisplayRoute(directionsService, directionsRenderer,new_end);
}
//******* Fim de Distance Matrix */
}
function calculateAndDisplayRoute(directionsService, directionsRenderer,new_end) {
directionsService
.route({
origin: new_end,
destination: "R. Euclídes Miragaia, 25 - Centro, São José dos Campos - SP, 12245-820, Brasil",
travelMode: google.maps.TravelMode.DRIVING,
})
.then(function (response) {
directionsRenderer.setDirections(response);
})
.catch(function (e) { return window.alert("Directions request failed due to " + status); });
}
geolocation();