fixphrase.com/js/map_maplibre.js

146 lines
4.2 KiB
JavaScript
Raw Normal View History

2021-05-23 00:05:57 -06:00
/*
* 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/.
*/
function maplibreMap() {
var theme = "liberty";
$("#mapbox").css("background-color", "#EFEFEF");
var map = new mapboxgl.Map({
container: 'mapbox',
2021-07-14 17:37:30 -06:00
style: "https://maps.netsyms.net/styles/osm-liberty-hillshading/style.json",
2021-05-23 00:05:57 -06:00
//attributionControl: false,
interactive: true,
pitch: 0,
zoom: 1,
maxZoom: 20,
2021-11-05 16:42:40 -06:00
continuousWorld: false,
noWrap: true,
2021-05-23 00:05:57 -06:00
center: [-97, 38]
});
map.on('click', function (e) {
var coordinates = e.lngLat;
try {
$('.mapboxgl-popup').remove();
var words = FixPhrase.encode(coordinates.lat, coordinates.lng);
var popup = new mapboxgl.Popup();
2021-11-05 16:42:40 -06:00
popup.setLngLat(e.lngLat);
popup.on('close', clearRectangle);
2021-11-05 16:51:11 -06:00
popup.setHTML("<b><span class='copyonclick'>" + words + "</span></b><br>" + (Math.round(coordinates.lat * 10000) / 10000) + ", " + (Math.round(coordinates.lng * 10000) / 10000));
popup.addTo(map);
drawRectangle(
coordinates.lat - (0.0001 / 2),
coordinates.lng - (0.0001 / 2),
coordinates.lat + (0.0001 / 2),
coordinates.lng + (0.0001 / 2)
);
2021-11-05 16:42:40 -06:00
map.flyTo({
center: e.lngLat,
zoom: Math.max(map.getZoom(), 14)
});
} catch (e) {
alert(e);
}
return;
2021-11-05 16:42:40 -06:00
// Example of accessing the PHP API instead of using JS
2021-05-23 00:05:57 -06:00
$.getJSON("lookup.php", {
latitude: coordinates.lat,
longitude: coordinates.lng
}, function (resp) {
$('.mapboxgl-popup').remove();
2021-07-14 17:34:53 -06:00
var popup = new mapboxgl.Popup();
popup.setLngLat(e.lngLat);
popup.setHTML("<b><span class='copyonclick'>" + resp.words + "</span></b><br>" + (Math.round(resp.coords[0] * 10000) / 10000) + ", " + (Math.round(resp.coords[1] * 10000) / 10000));
2021-07-14 17:34:53 -06:00
popup.addTo(map);
map.flyTo({
center: e.lngLat,
zoom: Math.max(map.getZoom(), 14)
});
2021-05-23 00:05:57 -06:00
});
});
map.addControl(new mapboxgl.NavigationControl({
visualizePitch: true
}), 'top-left');
map.addControl(new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true,
timeout: 10 * 1000
},
fitBoundsOptions: {
maxZoom: 16
},
trackUserLocation: true
}), 'top-left');
map.addControl(new mapboxgl.ScaleControl({
unit: "imperial"
}));
map.mapEasing = function (t) {
return t * (2 - t);
};
map.setMapHeading = function (heading) {
if (typeof heading == 'number') {
map.easeTo({
bearing: heading,
easing: map.mapEasing
});
}
};
map.setMapLocation = function (latitude, longitude) {
map.easeTo({
center: [
longitude,
latitude
]
});
};
map.animateMapIn = function (latitude, longitude, zoom, heading) {
if (typeof zoom == 'undefined') {
zoom = 10;
}
if (typeof heading == 'undefined') {
heading = 0;
}
map.flyTo({
center: [
longitude,
latitude
],
speed: 1,
zoom: zoom,
heading: heading,
pitch: 0
});
};
map.addMarker = function (latitude, longitude) {
var el = document.createElement("div");
el.className = "map-marker";
new mapboxgl.Marker(el).setLngLat([longitude, latitude]).addTo(map);
};
map.removeMarkers = function () {
var oldmarkers = document.getElementsByClassName("map-marker");
if (oldmarkers.length > 0) {
markerparent = oldmarkers[0].parentNode;
while (oldmarkers.length > 0) {
markerparent.removeChild(oldmarkers[0]);
}
}
}
return map;
}