-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance-computer.js
168 lines (146 loc) · 5.07 KB
/
distance-computer.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
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
164
165
166
167
168
'use strict';
/*** Init ***/
function initialize() {
// bounds
var bounds = new google.maps.LatLngBounds();
// map
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
mapTypeId: google.maps.MapTypeId.TERRAIN,
scrollwheel: false,
navigationControl: false,
mapTypeControl: false,
scaleControl: false,
draggable: true,
});
// Current location
var infoWindow = new google.maps.InfoWindow({map: map});
/*** Location error handler ***/
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('You are here.');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
/*** Variables ***/
//var geocoder = new google.maps.Geocoder();
var pickupAddress = document.getElementById("pickupAddress");
var dropAddress = document.getElementById("dropAddress");
var pickupPosition = document.getElementById("pickupPosition");
var dropPosition = document.getElementById("dropPosition");
var distance = document.getElementById("distance");
var path = new google.maps.Polyline({
path: [],
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
var autocompleteOptions = {
//componentRestrictions: {country: 'be'}
};
// Set pickup completion
var pickup = {
autocomplete: new google.maps.places.Autocomplete(pickupAddress, autocompleteOptions),
place: '',
name: '',
vicinity: '',
position: '',
marker: {}
};
// Set drop completion
var drop = {
autocomplete: new google.maps.places.Autocomplete(dropAddress, autocompleteOptions),
place: '',
name: '',
vicinity: '',
position: '',
marker: {}
};
/*** Functions ***/
// Compute distance
var computeDistance = function(pickup, drop){
if(pickup && drop){
var a = google.maps.geometry.spherical.computeDistanceBetween(pickup, drop) / 1000;
return a.toFixed(3);
} else {
return '-';
}
};
// Manage marker
var manageMarker = function(location, title, map){
// Remove marker and set previous marker
if(Object.keys(location.marker).length > 0){
location.previousMarker = location.marker;
location.marker.setMap(null);
}
// Add marker
var marker = new google.maps.Marker({
position: location.place.geometry.location,
map: map,
title: title,
label: title
});
location.marker = marker;
// Extend bound
bounds.extend(location.marker.position);
map.fitBounds(bounds);
};
// Manage path
var managePath = function(pickup, drop, map){
// Update path
if(Object.keys(pickup.marker).length > 0
&& Object.keys(drop.marker).length > 0){
var coordinates = [];
coordinates.push({lat: pickup.marker.getPosition().lat(), lng: pickup.marker.getPosition().lng()});
coordinates.push({lat: drop.marker.getPosition().lat(), lng: drop.marker.getPosition().lng()});
path.setPath(coordinates);
path.setMap(map);
}
};
/*** Event listeners ***/
// Pickup
google.maps.event.addListener(pickup.autocomplete, 'place_changed', function () {
// Set pickup
pickup.place = pickup.autocomplete.getPlace();
pickup.name = pickup.place.name;
pickup.vicinity = pickup.place.vicinity;
pickup.position = pickup.place.geometry.location;
// Manage marker
manageMarker(pickup, 'Pickup', map);
// Manage path
managePath(pickup, drop, map);
// Set pickup value
pickupPosition.value = pickup.position.toString()
// Compute distance
distance.value = computeDistance(pickup.position, drop.position);
});
// Drop
google.maps.event.addListener(drop.autocomplete, 'place_changed', function () {
// set drop
drop.place = drop.autocomplete.getPlace();
drop.name = drop.place.name;
drop.vicinity = drop.place.vicinity;
drop.position = drop.place.geometry.location;
// Manage marker
manageMarker(drop, 'Drop', map);
// Manage path
managePath(pickup, drop, map);
// Set pickup value
dropPosition.value = drop.position.toString()
// Compute distance
distance.value = computeDistance(pickup.position, drop.position);
});
}
google.maps.event.addDomListener(window, 'load', initialize);