Game/www/js/map.js

86 lines
1.9 KiB
JavaScript

/*
* 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/.
*/
//import mapboxgl from 'mapbox-gl';
// or "const mapboxgl = require('mapbox-gl');"
mapboxgl.accessToken = '';
const map = new mapboxgl.Map({
container: 'mapbox',
style: SETTINGS['map_style_json'],
attributionControl: false,
pitch: 0,
zoom: 0,
maxZoom: 18
});
map.touchZoomRotate.enable({around: 'center'});
map.dragPan.disable();
function mapEasing(t) {
return t * (2 - t);
}
gotfirstfix = false;
watchLocation(function (position) {
if (gotfirstfix) {
setMapLocation(position.coords.latitude, position.coords.longitude);
} else {
animateMapIn(position.coords.latitude, position.coords.longitude, 16, position.coords.heading);
gotfirstfix = true;
}
}, function (error) {
app.toast.show({
text: '<i class="fas fa-compass"></i> ' + error,
position: "bottom",
destroyOnClose: true,
closeTimeout: 5000
});
});
function setMapHeading(heading) {
if (typeof heading == 'number') {
map.easeTo({
bearing: heading,
easing: mapEasing
});
}
}
function setMapLocation(latitude, longitude) {
map.easeTo({
center: [
longitude,
latitude
]
});
}
function animateMapIn(latitude, longitude, zoom, heading) {
if (typeof zoom == 'undefined') {
zoom = 15;
}
if (typeof heading == 'undefined') {
heading = 0;
}
map.flyTo({
center: [
longitude,
latitude
],
speed: 0.7,
zoom: zoom,
heading: heading,
pitch: 45
});
// Set min zoom after some time to fly in
setTimeout(function () {
map.setMinZoom(15);
map.setPitch(45);
}, 1000);
}