diff --git a/servers/php/getallroutesformap.php b/servers/php/getallroutesformap.php new file mode 100644 index 0000000..4499e8f --- /dev/null +++ b/servers/php/getallroutesformap.php @@ -0,0 +1,34 @@ +multi_query($query)) { + + do { // build our json array + if ($result = $mysqli->store_result()) { + while ($row = $result->fetch_row()) { + $json .= $row[2]; + $json .= ','; + } + $result->close(); + } + } while ($mysqli->more_results() && $mysqli->next_result()); + } + else { + die('error: ' . $mysqli->error); + } + + $json = rtrim($json, ","); + $json .= '] }'; + + header('Content-Type: application/json'); + echo $json; + + $mysqli->close(); +?> \ No newline at end of file diff --git a/servers/php/js/maps.js b/servers/php/js/maps.js index f281537..c6145e2 100644 --- a/servers/php/js/maps.js +++ b/servers/php/js/maps.js @@ -6,6 +6,7 @@ var intervalID = 0; var zoom = 12; + getAllRoutesForMap(); load(); $("#routeSelect").change(function() { @@ -30,7 +31,25 @@ } else { turnOnAutoRefresh(); } - }); + }); + + function getAllRoutesForMap() { + // when the page first loads, get the routes from the DB and load them into the dropdown box. + $.ajax({ + url: 'getallroutesformap.php', + type: 'GET', + dataType: 'json', + success: function(data) { + // console.log(JSON.stringify(data)); + loadGPSLocations(data); + }, + error: function (xhr, status, errorThrown) { + //console.log("responseText in error: " + xhr.responseText); + console.log("error status: " + xhr.status); + console.log("errorThrown: " + errorThrown); + } + }); + } function load() { // when the page first loads, get the routes from the DB and load them into the dropdown box. @@ -161,25 +180,30 @@ var finalLocation = false; var counter = 0; + + var locationArray = []; // iterate through the locations and create map markers for each location $(json.locations).each(function(key, value){ + var latitude = $(this).attr('latitude'); + var longitude = $(this).attr('longitude'); + var tempLocation = new L.LatLng(latitude, longitude); + + locationArray.push(tempLocation); + counter++; // want to set the map center on the last location if (counter == $(json.locations).length) { - var longitude = $(this).attr('longitude'); - var latitude = $(this).attr('latitude'); - - gpsTrackerMap.setView(new L.LatLng(latitude, longitude), zoom); + gpsTrackerMap.setView(tempLocation, zoom); finalLocation = true; displayCityName(latitude, longitude); } var marker = createMarker( - $(this).attr('latitude'), - $(this).attr('longitude'), + latitude, + longitude, $(this).attr('speed'), $(this).attr('direction'), $(this).attr('distance'), @@ -191,6 +215,10 @@ $(this).attr('extraInfo'), gpsTrackerMap, finalLocation); }); + + // fit markers within window + var bounds = new L.LatLngBounds(locationArray); + gpsTrackerMap.fitBounds(bounds); } }