/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ var gotfirstfix = false; var lastMapUpdateTimestamp = 0; var playerPosition = { coords: { latitude: 0.0, longitude: 0.0, accuracy: 999999 } }; var geoerrorcount = 0; var gamemaptype = "mapbox"; if (localStorage.getItem("litemode") == "true") { gamemaptype = "leaflet"; } if (gamemaptype == "leaflet") { var map = leafletMap(); } else { var map = mapboxMap(); } // Make sure map places are refreshed even when location isn't var mapRefreshInterval = setInterval(function () { if (gotfirstfix) { var currentTimestamp = Math.floor(Date.now() / 1000); if (lastMapUpdateTimestamp < (currentTimestamp - 10)) { updatePlaceLayer(playerPosition.coords.latitude, playerPosition.coords.longitude); lastMapUpdateTimestamp = currentTimestamp; } } }, 30 * 1000); watchLocation(function (position) { playerPosition = position; if (gotfirstfix) { setMapLocation(position.coords.latitude, position.coords.longitude); // Don't refresh map at an interval less than ten seconds var currentTimestamp = Math.floor(Date.now() / 1000); if (lastMapUpdateTimestamp < (currentTimestamp - 10)) { updatePlaceLayer(position.coords.latitude, position.coords.longitude); lastMapUpdateTimestamp = currentTimestamp; } } else { animateMapIn(position.coords.latitude, position.coords.longitude, 16, position.coords.heading); gotfirstfix = true; } }, function (error) { geoerrorcount++; console.log("Geolocation error #" + geoerrorcount + ": ", error); // Stop showing error toasts if they're happening a lot // if (geoerrorcount > 3) { // return; // } // app.toast.show({ // text: ' ' + error, // position: "bottom", // destroyOnClose: true, // closeTimeout: 1000 * 4 // }); }); function setMapHeading(heading) { map.setHeading(heading); } function setMapLocation(latitude, longitude) { map.setMapLocation(latitude, longitude); } function updatePlaceLayer(latitude, longitude) { callAPI("nearbyplaces", { latitude: latitude, longitude: longitude, radius: 2 }, function (data) { map.updatePlaceLayer(data); }); } function animateMapIn(latitude, longitude, zoom, heading) { if (typeof zoom == 'undefined') { zoom = 17; } if (typeof heading == 'undefined') { heading = 0; } map.animateMapIn(latitude, longitude, zoom, heading); }