-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapScript.js
More file actions
165 lines (135 loc) · 4.45 KB
/
Copy pathmapScript.js
File metadata and controls
165 lines (135 loc) · 4.45 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var fromLat = 0.00;
var fromLng = 0.00;
var toLat = 0.00;
var toLng = 0.00;
var map;
(function () {
var placesAutocomplete = places({
appId: 'plKZWQZ4OYQ8',
apiKey: '6b884ab3c793e4319219fc9e58d16f67',
container: document.querySelector('#comingFrom')
}).configure({
type: 'city',
aroundLatLngViaIP: false,
});
placesAutocomplete.on("change", function resultSelected(e) {
fromLat = parseFloat(e.suggestion.latlng.lat);
fromLng = parseFloat(e.suggestion.latlng.lng);
console.log(fromLat + ',' + fromLng);
});
})();
(function () {
var placesAutocomplete = places({
appId: 'plKZWQZ4OYQ8',
apiKey: '6b884ab3c793e4319219fc9e58d16f67',
container: document.querySelector('#goingTo')
}).configure({
type: 'city',
aroundLatLngViaIP: false,
});
placesAutocomplete.on("change", function resultSelected(e) {
toLat = parseFloat(e.suggestion.latlng.lat);
toLng = parseFloat(e.suggestion.latlng.lng);
console.log(toLat + ',' + toLng);
});
})();
function displayMap() {
if (map) {
map.off();
map.remove();
}
var placesAutocomplete = places({
appId: 'plKZWQZ4OYQ8',
apiKey: '6b884ab3c793e4319219fc9e58d16f67',
container: document.querySelector('#input-map')
});
//var latlng = L.latLng(39.952583, -75.165222);
map = L.map('map-example-container', {
center: L.latLng(0, 0),
zoom: 4
});
var osmLayer = new L.TileLayer(
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
minZoom: 3,
maxZoom: 17,
attribution: 'Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors'
}
);
var markers = [];
var fromMarker = L.marker([fromLat, fromLng], { title: 'Your Starting Destination' }).addTo(map).bindPopup('Departing from')
.openPopup();
var toMarker = L.marker([toLat, toLng]).addTo(map).bindPopup('Your Destination')
.openPopup();
markers.push(fromMarker);
markers.push(toMarker);
console.log(markers);
var latlngs = [
[fromLat, fromLng],
[toLat, toLng]
];
var polyline = L.polyline(latlngs, { color: 'red' }).addTo(map);
// zoom the map to the polyline
map.fitBounds(polyline.getBounds());
//map.setView(new L.LatLng(50.5, 30.5), 1);
map.addLayer(osmLayer);
placesAutocomplete.on('suggestions', handleOnSuggestions);
placesAutocomplete.on('cursorchanged', handleOnCursorchanged);
placesAutocomplete.on('change', handleOnChange);
placesAutocomplete.on('clear', handleOnClear);
// markers.on('click', function(e) {
// var zoomLat = $(this).latlng.lat;
// var zoomLng = $(this).latlng.lng;
// var zoomZ = [zoomLat, zoomLng]
// map.fitBounds(zoomZ.getBounds());
// })
function handleOnSuggestions(e) {
markers.forEach(removeMarker);
markers = [];
if (e.suggestions.length === 0) {
map.setView(new L.LatLng(0, 0), 1);
return;
}
e.suggestions.forEach(addMarker);
findBestZoom();
}
function handleOnChange(e) {
markers
.forEach(function (marker, markerIndex) {
if (markerIndex === e.suggestionIndex) {
markers = [marker];
marker.setOpacity(1);
findBestZoom();
} else {
removeMarker(marker);
}
});
}
function handleOnClear() {
map.setView(new L.LatLng(0, 0), 1);
markers.forEach(removeMarker);
}
function handleOnCursorchanged(e) {
markers
.forEach(function (marker, markerIndex) {
if (markerIndex === e.suggestionIndex) {
marker.setOpacity(1);
marker.setZIndexOffset(1000);
} else {
marker.setZIndexOffset(0);
marker.setOpacity(0.5);
}
});
}
function addMarker(suggestion) {
var marker = L.marker(suggestion.latlng, { opacity: .4 });
marker.addTo(map);
markers.push(marker);
}
function removeMarker(marker) {
map.removeLayer(marker);
}
function findBestZoom() {
var featureGroup = L.featureGroup(markers);
map.fitBounds(featureGroup.getBounds().pad(0.5), { animate: false });
}
}