/
index.php
244 lines (205 loc) · 5.87 KB
/
index.php
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
width: 90%;
height: 90%;
//margin: 0px;
//padding: 0px
}
a{
cursor: pointer;
text-decoration: underline;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script>
// map
var poly = '';
var map;
var markeruser = '';
var markerdestination = '';
// boolean
var __global_user = false;
var __global_destination = false;
var update_timeout;
// temporary list angkot
var temp_list_angkot = [];
/**
* INITIALIZE GOOGLE MAP
*/
function initialize() {
/* setup map */
var mapOptions = {
zoom: 13,
center: new google.maps.LatLng(-6.2858667, 106.8719382)
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
/* create marker and line by click */
google.maps.event.addListener(map, 'click', function(event)
{
icons = 'http://latcoding.com/domains/dijkstra.latcoding.com/imgs/user_min.png';
var location = event.latLng;
update_timeout = setTimeout(function()
{
if(__global_user == false){
markeruser = new google.maps.Marker({
position: location,
map: map,
icon: icons,
draggable: true,
title: 'test drag',
});
// update
__global_user = true;
}else{
markeruser.setPosition(location);
}
}, 200);
});
// handle click and dblclick same time
google.maps.event.addListener(map, 'dblclick', function(event) {
clearTimeout(update_timeout);
});
}
/**
* PILIH DESTINATION (SEKOLAH) VIA <SELECT>
*/
function choose_destination(value){
// teks option
var teks = $("#select_tujuan option:selected").text();
// -- PILIH -- dipilih
if(value == 'pilih') return false;
// reset polyline
if(poly != '') poly.setMap(null);
// RESET ANGKOT SEBELUMNYA
$(temp_list_angkot).each(function(w, x){
// x = marker0, marker1 dst
window[x].setMap(null);
});
var location = JSON.parse(value);
icons = 'http://latcoding.com/domains/dijkstra.latcoding.com/imgs/school_24.png';
if(__global_destination == false){
markerdestination = new google.maps.Marker({
position: location,
map: map,
icon: icons,
draggable: false,
title: 'TUJUAN : ' + teks,
});
__global_destination = true;
}else{
markerdestination.setPosition(location);
markerdestination.setTitle('TUJUAN : ' + teks);
}
}
/**
* GET JSON DIJSKTRA VIA AJAX
*/
function send_dijkstra(){
if(markeruser == '' || markerdestination == ''){
alert('Isi dulu koordinat user & tujuan');
return false;
}
console.log(markeruser.position.lat());
console.log(markeruser.position.lng());
now_koord_user = '{"lat": ' + markeruser.position.lat() + ', "lng": ' + markeruser.position.lng() + '}';
now_koord_destination = '{"lat": ' + markerdestination.position.lat() + ', "lng": ' + markerdestination.position.lng() + '}';
// loading
$('#run_dijkstra').hide();
$('#loading').show();
$.ajax({
method:"POST",
url : "Main.php",
data: {koord_user: now_koord_user, koord_destination: now_koord_destination},
success:function(response){
// remove loading
$('#run_dijkstra').show();
$('#loading').hide();
var json = JSON.parse(response);
console.log(response);
// RESET POLYLINE
if(poly != '') poly.setMap(null);
// RESET ANGKOT SEBELUMNYA
$(temp_list_angkot).each(function(w, x){
// x = marker0, marker1 dst
window[x].setMap(null);
});
// ERROR ALGORITMA DIJKSTRA
if(json.hasOwnProperty("error")) alert(json['error']['teks']);
// GAMBAR JALUR SHORTEST PATH
/* setup polyline */
var polyOptions = {
/*path: [
{"lat": 37.772, "lng": -122.214},
{"lat": 21.291, "lng": -157.821},
{"lat": -18.142, "lng": 178.431},
{"lat": -27.467, "lng": 153.027}],
*/
path: json['jalur_shortest_path'],
geodesic: true,
strokeColor: 'rgb(20, 120, 218)',
strokeOpacity: 1.0,
strokeWeight: 2,
};
poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
// GAMBAR KOORDINAT ANGKOT
$(json['angkot']).each(function(i, v)
{
// no_angkot
no_angkot = JSON.stringify(v['no_angkot']);
window['infowindow'+i] = new google.maps.InfoWindow({
content: '<div>'+ no_angkot +'</div>'
});
// koordinat angkot
koordinat_angkot = v['koordinat_angkot'];
window['marker'+i] = new google.maps.Marker({
position: koordinat_angkot,
map: map,
title: 'title',
icon: 'http://latcoding.com/free_download/implementasi_dijkstra_di_android/car.png'
});
// popup
window['marker'+i].addListener('click', function() {
window['infowindow'+i].open(map, window['marker'+i]);
});
// temporary list angkot
temp_list_angkot[i] = 'marker'+i;
});
},
error:function(er){
alert('error: '+er);
// remove loading
$('#run_dijkstra').show();
$('#loading').hide();
}
});
}
/* load google maps v3 */
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<?php
include "Main.php";
// koneksi
$m = new Main();
$koneksi = $m->koneksi;
// query
$sql = "SELECT * FROM sekolah";
$query = mysqli_query($koneksi, $sql);
// select option
echo 'TUJUAN : <select id="select_tujuan" onchange="choose_destination(this.value)">';
echo '<option value="pilih">-- PILIH --</option>';
while($fetch = mysqli_fetch_array($query, MYSQLI_ASSOC))
{
$koordinat = $fetch['koordinat'];
$exp_koordinat = explode(',', $koordinat);
$json_koordinat = '{"lat": '.$exp_koordinat[0].', "lng": '.$exp_koordinat[1].'}';
echo "<option value='$json_koordinat'>$fetch[tujuan]</option>";
}
echo '</select>';
?>
<span><button onclick="send_dijkstra()" id='run_dijkstra'>RUN</button><span id='loading' style='display:none'>membuat rute ..</span></span>
<div id="map-canvas" style="float:left;"></div>
<div id='DEBUG'></div>