-
Notifications
You must be signed in to change notification settings - Fork 1
/
closest bike station.html
196 lines (161 loc) · 5.59 KB
/
closest bike station.html
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html>
<head>
<title>Navigation functions (heading)</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
#panel {
position: absolute;
top: 5px;
left: 50%;
margin-left: -180px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
//Additional variables
var startPos;
var endPos;
function initialize() {
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(34, -40.605)
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
map.controls[google.maps.ControlPosition.TOP_CENTER].push(
document.getElementById('info'));
//enter start position
startPos = new google.maps.LatLng(43.642946, -79.394033);
endPos = new google.maps.LatLng(43.653921, -79.373217);
$.getJSON( "stations.json", function( data ) {
data.forEach( function(station) {
var marker = new google.maps.Marker({
map: map,
draggable: true,
position: new google.maps.LatLng(station.Latitude, station.Longitude)
});
});
});
DetermineClosestStation(startPos, function (closestStation) {
DetermineClosestStation(endPos, function(closestStationEnd) {
console.log ([closestStation, closestStationEnd]);
marker1 = new google.maps.Marker({
map: map,
draggable: true,
position: new google.maps.LatLng(closestStation.Latitude, closestStation.Longitude)
});
marker2 = new google.maps.Marker({
map: map,
draggable: true,
position: new google.maps.LatLng(closestStationEnd.Latitude, closestStationEnd.Longitude)
//43.647992, -79.370907)
});
var bounds = new google.maps.LatLngBounds(marker1.getPosition(),
marker2.getPosition());
map.fitBounds(bounds);
google.maps.event.addListener(marker1, 'position_changed', update);
google.maps.event.addListener(marker2, 'position_changed', update);
var polyOptions = {
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 3,
map: map,
};
poly = new google.maps.Polyline(polyOptions);
var geodesicOptions = {
strokeColor: '#CC0099',
strokeOpacity: 1.0,
strokeWeight: 3,
geodesic: true,
map: map
};
geodesicPoly = new google.maps.Polyline(geodesicOptions);
update();
}
);
}
);
/*new google.maps.Marker({
map: map,
draggable: true,
position: new google.maps.LatLng(40.71435280, -74.0059731)
});
marker2 = new google.maps.Marker({
map: map,
draggable: true,
position: new google.maps.LatLng(closestStation.Latitude, closestStation.Longitude)
//43.647992, -79.370907)
});
*/
}
function update() {
var path = [marker1.getPosition(), marker2.getPosition()];
poly.setPath(path);
geodesicPoly.setPath(path);
var heading = google.maps.geometry.spherical.computeHeading(path[0],
path[1]);
document.getElementById('heading').value = heading;
document.getElementById('origin').value = path[0].toString();
document.getElementById('destination').value = path[1].toString();
}
function DetermineClosestStation(position, cb) {
//load dock data
var closestStation = null;
var closestStationDist = 0;
/*
$.ajax({
dataType: 'json',
url: "livestations.json",
success: function(data) {
console.log(data);
data.stationBeanList.forEach( function(station) {
var stationLatLng = new google.maps.LatLng(station.latitude, station.longitude);
console.log(stationLatLng);
var distance = google.maps.geometry.spherical.computeDistanceBetween(stationLatLng, position);
if (!closestStation || closestStationDist > distance)
{
closestStationDist = distance;
closestStation = station;
console.log("calculating closest: " + closestStation.stationName + ", " + "closestStationDist");
}
});
}
});
*/
$.getJSON( "stations.json", function( data ) {
console.log(data);
data.forEach( function(station) {
var stationLatLng = new google.maps.LatLng(station.Latitude, station.Longitude);
// console.log(stationLatLng);
var distance = google.maps.geometry.spherical.computeDistanceBetween(stationLatLng, position);
if (!closestStation || closestStationDist > distance)
{
closestStationDist = distance;
closestStation = station;
console.log("calculating closest: " + closestStation + ", " + "closestStationDist");
}
});
console.log("returning");
cb(closestStation);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
<input id="pac-input" class="controls" type="text" placeholder="Search Box" autocomplete="off" style="z-index: ; position: absolute; left: 42px; top: 0px;">
</body>
</html>