jQuery(document).ready(function($) { var pluginUrl = map_js_vars.plugin_url; var selectRoute = document.getElementById('selectRoute'); var map = document.getElementById('map-canvas'); var intervalID = 0; var zoom = 12; var autoRefresh = false; var sessionIDArray = []; var viewingAllRoutes = false; getAllRoutesForMap(); loadRoutesIntoDropdownBox(); $("#viewall").click(function() { getAllRoutesForMap(); }); $('#selectRoute').on('change', function() { if (hasMap()) { viewingAllRoutes = false; getRouteForMap(); } }); $('#autorefresh').click(function() { if (autoRefresh) { turnOffAutoRefresh(); } else { turnOnAutoRefresh(); } }); $("#delete").click(function() { if (hasMap()) { deleteRoute(); } }); $("#refresh").click(function() { if (viewingAllRoutes) { getAllRoutesForMap(); } else { if (hasMap()) { getRouteForMap(); } } }); function setTheme() { //var bodyBackgroundColor = $('body').css('backgroundColor'); //$('.container').css('background-color', bodyBackgroundColor); //$('body').css('background-color', '#ccc'); // $('head').append(''); } function getAllRoutesForMap() { // when the page first loads, get the routes from the DB and load them into the dropdown box. viewingAllRoutes = true; //selectRoute.selectedIndex = 0; showPermanentMessage('Please select a route below'); $.post( map_js_vars.ajax_url, { 'action': 'get_all_geojson_routes', 'get_all_geojson_routes_nonce': map_js_vars.get_all_geojson_routes_nonce }, function(response) { loadGPSLocations(response); } ); } function loadRoutesIntoDropdownBox() { $.post( map_js_vars.ajax_url, { 'action': 'get_routes', 'get_routes_nonce': map_js_vars.get_routes_nonce }, function(response) { loadRoutes(response); }); } function loadRoutes(json) { // console.log(JSON.stringify(json)); if (json.length == 0 || json == '0') { showMessage('There are no routes available to view'); map.innerHTML = ''; } else { // create the first option of the dropdown box var option = document.createElement('option'); option.setAttribute('value', '0'); option.innerHTML = 'Select Route...'; selectRoute.appendChild(option); // when a user taps on a marker, the position of the sessionID in this array is the position of the route // in the dropdown box. it's used below to set the index of the dropdown box when the map is changed sessionIDArray = []; // iterate through the routes and load them into the dropdwon box. $(json.routes).each(function(key, value){ var option = document.createElement('option'); option.setAttribute('value', $(this).attr('session_id')); option.innerHTML = $(this).attr('user_name') + " " + $(this).attr('times'); selectRoute.appendChild(option); sessionIDArray.push($(this).attr('session_id')); }); // need to reset this for firefox selectRoute.selectedIndex = 0; showPermanentMessage('Please select a route below'); } } function getRouteForMap() { $.post( map_js_vars.ajax_url, { 'action': 'get_geojson_route', 'session_id': $('#selectRoute').val(), 'get_geojson_route_nonce': map_js_vars.get_geojson_route_nonce }, function(response) { loadGPSLocations(response); } ); } function loadGPSLocations(geojson) { // console.log(JSON.stringify(geojson)); if (geojson.length == 0 || geojson == '0') { showMessage('There is no tracking data to view'); map.innerHTML = ''; } else { var finalLocation = false; if (map.id == 'map-canvas') { // clear any old map objects document.getElementById('map-canvas').outerHTML = "
"; // use leaflet (http://leafletjs.com/) to create our map and map layers var gpsTrackerMap = new L.map('map-canvas'); var openStreetMapsURL = ('https:' == document.location.protocol ? 'https://' : 'http://') + '{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'; var openStreetMapsLayer = new L.TileLayer(openStreetMapsURL, {attribution:'©2014 OpenStreetMap contributors'}); // need to get your own bing maps key, http://www.microsoft.com/maps/create-a-bing-maps-key.aspx var bingMapsLayer = new L.BingLayer("AnH1IKGCBwAiBWfYAHMtIfIhMVybHFx2GxsReNP5W0z6P8kRa67_QwhM4PglI9yL"); var googleMapsLayer = new L.Google('ROADMAP'); // this fixes the zoom buttons from freezing // https://github.com/shramov/leaflet-plugins/issues/62 L.polyline([[0, 0], ]).addTo(gpsTrackerMap); // this sets which map layer will first be displayed, go ahead and change it to bingMapsLayer or openStreetMapsLayer to see gpsTrackerMap.addLayer(googleMapsLayer); // this is the switcher control to switch between map types (upper right hand corner of map) gpsTrackerMap.addControl(new L.Control.Layers({ 'Bing Maps':bingMapsLayer, 'Google Maps':googleMapsLayer, 'OpenStreetMaps':openStreetMapsLayer }, {})); } var locationArray = []; for (var i = 0; i < $(geojson.features).length; i++) { var longitude = geojson.features[i].geometry.coordinates[0]; var latitude = geojson.features[i].geometry.coordinates[1]; var tempLocation = new L.LatLng(latitude, longitude); locationArray.push(tempLocation); if (i == ($(geojson.features).length) - 1) { //gpsTrackerMap.setView(new L.LatLng(latitude, longitude), zoom); finalLocation = true; if (!viewingAllRoutes) { displayCityName(latitude, longitude); } } var marker = createMarker( latitude, longitude, geojson.features[i].id, // session_id geojson.features[i].properties.speed, geojson.features[i].properties.direction, geojson.features[i].properties.distance, geojson.features[i].properties.location_method, geojson.features[i].properties.gps_time, geojson.features[i].properties.user_name, geojson.features[i].properties.accuracy, geojson.features[i].properties.extra_info, gpsTrackerMap, finalLocation); }; // fit markers within window var bounds = new L.LatLngBounds(locationArray); gpsTrackerMap.fitBounds(bounds); // restarting interval here in case we are coming from viewing all routes if (autoRefresh) { restartInterval(); } } } // check to see if we have a map loaded, don't want to autoRefresh or delete without it function hasMap() { if (selectRoute.selectedIndex == 0) { // means no map return false; } else { return true; } } function createMarker(latitude, longitude, session_id, speed, direction, distance, locationMethod, gpsTime, userName, accuracy, extraInfo, map, finalLocation) { var iconUrl; if (finalLocation) { iconUrl = pluginUrl + 'public/assets/images/coolred_small.png'; } else { iconUrl = pluginUrl + 'public/assets/images/coolgreen2_small.png'; } var markerIcon = new L.Icon({ iconUrl: iconUrl, shadowUrl: pluginUrl + 'public/assets/images/coolshadow_small.png', iconSize: [12, 20], shadowSize: [22, 20], iconAnchor: [6, 20], shadowAnchor: [6, 20], popupAnchor: [-3, -25] }); var lastMarker = ""; // when a user clicks on last marker, let them know it's final one if (finalLocation) { lastMarker = '" +
" | ||
Speed: | " + speed + " mph | |
Distance: | " + distance + " mi | |
Time: | " + gpsTime + " | |
UserName: | " + userName + " | |
Accuracy: | " + accuracy + " ft |