Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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('<link rel="stylesheet"
href="style2.css" type="text/css" />');
}
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));
sessionIDArray.push($
(this).attr('session_id'));
});
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 (map.id == 'map-canvas') {
// clear any old map objects
document.getElementById('map-
canvas').outerHTML = "<div id='map-canvas'></div>";
'{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var openStreetMapsLayer = new
L.TileLayer(openStreetMapsURL,
{attribution:'©2014 <a
href="http://openstreetmap.org">OpenStreetMap</a>
contributors'});
gpsTrackerMap.addLayer(googleMapsLayer);
'OpenStreetMaps':openStreetMapsLayer
}, {}));
}
if (i == ($(geojson.features).length)
- 1) {
//gpsTrackerMap.setView(new
L.LatLng(latitude, longitude), zoom);
finalLocation = true;
if (!viewingAllRoutes) {
displayCityName(latitude,
longitude);
}
}
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);
};
var iconUrl;
if (finalLocation) {
iconUrl = pluginUrl +
'public/assets/images/coolred_small.png';
} else {
iconUrl = pluginUrl +
'public/assets/images/coolgreen2_small.png';
}
gpstrackerMarker.on("click", function() {
viewingAllRoutes = false;
var indexOfRouteInRouteSelectDropdwon
= sessionIDArray.indexOf(session_id) + 1;
selectRoute.selectedIndex =
indexOfRouteInRouteSelectDropdwon;
showPermanentMessage('Please select a
route below');
if (autoRefresh) {
restartInterval();
}
$.post(
map_js_vars.ajax_url,
{
'action': 'get_geojson_route',
'session_id': session_id,
'get_geojson_route_nonce':
map_js_vars.get_geojson_route_nonce
},
function(response) {
loadGPSLocations(response);
}
);
}); // on click
}
}
function getCompassImage(azimuth) {
if ((azimuth >= 337 && azimuth <= 360) ||
(azimuth >= 0 && azimuth < 23))
return 'compassN';
if (azimuth >= 23 && azimuth < 68)
return 'compassNE';
if (azimuth >= 68 && azimuth < 113)
return 'compassE';
if (azimuth >= 113 && azimuth < 158)
return 'compassSE';
if (azimuth >= 158 && azimuth < 203)
return 'compassS';
if (azimuth >= 203 && azimuth < 248)
return 'compassSW';
if (azimuth >= 248 && azimuth < 293)
return 'compassW';
if (azimuth >= 293 && azimuth < 337)
return 'compassNW';
return "";
}
if (status == google.maps.GeocoderStatus.OK) {
// results[0] is full address
if (results[1]) {
reverseGeocoderResult =
results[1].formatted_address;
showPermanentMessage(reverseGeocoderResult);
} else {
console.log('No results found');
}
} else {
console.log('Geocoder failed due to: '
+ status);
}
});
}
function deleteRoute() {
// comment out these two lines to get
delete working
// var answer = confirm("Disabled here
on test website, this works fine.");
// return false;
if (answer){
$.post(
map_js_vars.ajax_url,
{
'action': 'delete_route',
'session_id': $
("#selectRoute").val(),
'delete_route_nonce':
map_js_vars.delete_route_nonce
},
function(response) {
map.innerHTML = '';
selectRoute.length = 0;
loadRoutesIntoDropdownBox();
getAllRoutesForMap();
}
);
}
else {
return false;
}
}
$('#messages').html(message);
setTimeout(function() {
$('#messages').html(tempMessage);
}, 7 * 1000); // 7 seconds
}
function showPermanentMessage(message) {
$('#messages').html(message);
}
function turnOffAutoRefresh() {
showMessage('Auto Refresh Off');
$('#autorefresh').val('Auto Refresh Off');
autoRefresh = false;
clearInterval(intervalID);
}
function turnOnAutoRefresh() {
showMessage('Auto Refresh On (1 min)');
$('#autorefresh').val('Auto Refresh On');
autoRefresh = true;
restartInterval();
}
function restartInterval() {
// remember that if someone is viewing all
routes and then switches to a single route
// while autorefresh is on then the
setInterval is going to be running with
getAllRoutesForMap
// and not getRouteForMap
clearInterval(intervalID);
if (viewingAllRoutes) {
intervalID =
setInterval(getAllRoutesForMap, 60 * 1000); // one
minute
} else {
intervalID = setInterval(getRouteForMap,
60 * 1000);
}
}
// for debugging,
console.log(objectToString(map));
function objectToString (obj) {
var str = '';
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str += p + ': ' + obj[p] + '\n';
}
}
return str;
}
});