-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
124 lines (110 loc) · 4.85 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function initMap() {
var sanDiego = {lat:32.7157, lng:-117.1611};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: sanDiego,
});
var infos = [];
var pos;
var geoLocationWindow = new google.maps.InfoWindow;
var geoLocationMarker = new google.maps.Marker;
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('right-panel'));
const myPromise = new Promise((resolve, reject) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
geoLocationMarker.setPosition(pos);
geoLocationMarker.setMap(map);
geoLocationWindow.setPosition(pos);
geoLocationWindow.setContent('Current location');
geoLocationWindow.open(map, geoLocationMarker);
map.setCenter(pos);
resolve(pos);
});
}
});
$(document).ready(function() {
$.getJSON('data.json', function(data) {
$.each(data, function(i, value) {
$('table > tbody').append('<tr></tr>');
$('table > tbody > tr:last').append('<td>' + value.name + '</td>').append('<td>' + value.description + '</td>').append('<td><a href="https://www.google.com/maps?q=' + value.location + '" target="blank">Open in Google Maps</td>');
$('#destination').append('<option></option>');
$('option:last').attr('value', value.location).append(value.name);
var latLng = new google.maps.LatLng(value.location[0], value.location[1]);
var marker = new google.maps.Marker({
position: latLng,
map: map,
});
var contentString = '<h3>' + value.name + '</h3>' + '<p>' + value.description + '</p>';
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.DistanceMatrixService();
var htmlTable = $('table > tbody > tr').eq(i).find('td').eq(2);
myPromise.then((success) => {
service.getDistanceMatrix({
origins: [success],
destinations: [latLng],
travelMode: 'DRIVING',
}, callback);
function callback(response, status) {
if (status === 'OK') {
var results = response.rows[0].elements[0];
htmlTable.append('<p>Distance: ' + results.distance.text + '</p>');
} else {
alert('Error was: ' + status);
}
};
});
google.maps.event.addListener(marker, 'mouseover', (function(marker, contentString, infowindow) {
return function() {
closeInfos();
infowindow.setContent(contentString);
infowindow.open(map, marker);
infos[0] = infowindow;
};
})(marker, contentString, infowindow));
})
});
});
var onChangeHandler = function() {
if (document.getElementById('destination').value == "") {
return;
}
calculateAndDisplayRoute(directionsService, directionsDisplay);
};
document.getElementById('destination').addEventListener('change', onChangeHandler);
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
var destinationValue = (document.getElementById('destination').value.split(','));
var lat = parseFloat(destinationValue[0]);
var lng = parseFloat(destinationValue[1]);
var latLngValue = {lat: lat, lng: lng};
directionsService.route({
origin: pos,
destination: latLngValue,
travelMode: 'DRIVING'
}, function(response, status) {
if (status=== 'OK') {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
};
function closeInfos() {
if (infos.length > 0) {
infos[0].set('marker', null);
infos[0].close();
infos.length = 0;
//close old info window after new info window is opened via event handler
}
};
};
/*
style webpage and table
then sort top spots based of distance
*/