mapcomplete/Logic/Leaflet/Basemap.ts

64 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-09-27 20:48:43 +00:00
import * as L from "leaflet"
import {UIEventSource} from "../UIEventSource";
2020-07-29 22:59:08 +00:00
import {UIElement} from "../../UI/UIElement";
import {BaseLayer} from "../../Models/BaseLayer";
import AvailableBaseLayers from "../Actors/AvailableBaseLayers";
import Loc from "../../Models/Loc";
2020-06-23 22:35:19 +00:00
2020-07-22 12:46:43 +00:00
export class Basemap {
2020-06-23 22:35:19 +00:00
2020-07-22 12:46:43 +00:00
// @ts-ignore
public readonly map: Map;
2020-06-23 22:35:19 +00:00
public readonly LastClickLocation: UIEventSource<{ lat: number, lon: number }> = new UIEventSource<{ lat: number, lon: number }>(undefined)
public readonly CurrentLayer: UIEventSource<BaseLayer> = new UIEventSource(AvailableBaseLayers.osmCarto);
2020-06-23 22:35:19 +00:00
2020-06-29 01:12:44 +00:00
constructor(leafletElementId: string,
location: UIEventSource<Loc>,
2020-06-29 01:12:44 +00:00
extraAttribution: UIElement) {
this.map = L.map(leafletElementId, {
center: [location.data.lat ?? 0, location.data.lon ?? 0],
zoom: location.data.zoom ?? 2,
layers: [ AvailableBaseLayers.osmCarto.layer],
2020-06-23 22:35:19 +00:00
});
2020-11-20 10:31:54 +00:00
L.control.scale(
{
position: 'topright',
}
).addTo(this.map)
// Users are not allowed to zoom to the 'copies' on the left and the right, stuff goes wrong then
// We give a bit of leeway for people on the edges
// Also see: https://www.reddit.com/r/openstreetmap/comments/ih4zzc/mapcomplete_a_new_easytouse_editor/g31ubyv/
this.map.setMaxBounds(
[[-100,-200],[100,200]]
);
2020-06-29 01:40:19 +00:00
this.map.attributionControl.setPrefix(
extraAttribution.Render() + " | <a href='https://osm.org'>OpenStreetMap</a>");
this.map.zoomControl.setPosition("bottomright");
2020-06-23 22:35:19 +00:00
const self = this;
2020-06-23 22:35:19 +00:00
this.map.on("moveend", function () {
location.data.zoom = self.map.getZoom();
location.data.lat = self.map.getCenter().lat;
2020-06-29 01:12:44 +00:00
location.data.lon = self.map.getCenter().lng;
2020-06-23 22:35:19 +00:00
location.ping();
});
2020-09-27 20:48:43 +00:00
2020-06-29 01:12:44 +00:00
this.map.on("click", function (e) {
self.LastClickLocation.setData({lat: e.latlng.lat, lon: e.latlng.lng})
});
this.map.on("contextmenu", function (e) {
self.LastClickLocation.setData({lat: e.latlng.lat, lon: e.latlng.lng});
e.preventDefault();
});
2020-06-23 22:35:19 +00:00
}
2020-06-23 22:35:19 +00:00
}