-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathmap.js
116 lines (92 loc) · 3.95 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
import { Position } from './model/Position.js';
// Import controls
import { CollectionControl } from './controls/collection_control.js';
import { CoordinatesControl } from './controls/coordinates_control.js';
import { LocalCoordinatesControl } from './controls/local_coordinates_control.js';
import { RegionBaseCoordinatesControl } from './controls/region_base_coordinates_control.js';
import { GridControl } from './controls/grid_control.js';
import { LocationLookupControl } from './controls/location_lookup_control.js';
import { MapLabelControl } from './controls/map_label_control.js';
import { PlaneControl } from './controls/plane_control.js';
import { RegionLabelsControl } from './controls/region_labels_control.js';
import { RegionLookupControl } from './controls/region_lookup_control.js';
import { TitleLabel } from './controls/title_label.js';
import { Region } from './model/Region.js';
$(document).ready(function () {
const currentUrl = new URL(window.location.href);
const urlCentreX = currentUrl.searchParams.get("centreX");
const urlCentreY = currentUrl.searchParams.get("centreY");
const urlCentreZ = currentUrl.searchParams.get("centreZ");
const urlZoom = currentUrl.searchParams.get("zoom");
const urlRegionID = currentUrl.searchParams.get("regionID");
var map = L.map('map', {
//maxBounds: L.latLngBounds(L.latLng(-40, -180), L.latLng(85, 153))
zoomControl: false,
renderer: L.canvas()
});
map.plane = 0;
map.updateMapPath = function () {
if (map.tile_layer !== undefined) {
map.removeLayer(map.tile_layer);
}
map.tile_layer = L.tileLayer('https://raw.githubusercontent.com/Explv/osrs_map_tiles/master/' + map.plane + '/{z}/{x}/{y}.png', {
minZoom: 4,
maxZoom: 11,
attribution: 'Map data',
noWrap: true,
tms: true
});
map.tile_layer.addTo(map);
map.invalidateSize();
}
map.updateMapPath();
map.getContainer().focus();
map.addControl(new TitleLabel());
map.addControl(new CoordinatesControl());
map.addControl(new RegionBaseCoordinatesControl());
map.addControl(new LocalCoordinatesControl());
map.addControl(L.control.zoom());
map.addControl(new PlaneControl());
map.addControl(new LocationLookupControl());
map.addControl(new MapLabelControl());
map.addControl(new CollectionControl({ position: 'topright' }));
map.addControl(new RegionLookupControl());
map.addControl(new GridControl());
map.addControl(new RegionLabelsControl());
var prevMouseRect, prevMousePos;
map.on('mousemove', function (e) {
var mousePos = Position.fromLatLng(map, e.latlng, map.plane);
if (prevMousePos !== mousePos) {
prevMousePos = mousePos;
if (prevMouseRect !== undefined) {
map.removeLayer(prevMouseRect);
}
prevMouseRect = mousePos.toLeaflet(map);
prevMouseRect.addTo(map);
}
});
const setUrlParams = () => {
const mapCentre = map.getBounds().getCenter()
const centrePos = Position.fromLatLng(map, mapCentre, map.plane);
const zoom = map.getZoom();
window.history.replaceState(null, null, `?centreX=${centrePos.x}¢reY=${centrePos.y}¢reZ=${centrePos.z}&zoom=${zoom}`);
};
map.on('move', setUrlParams);
map.on('zoom', setUrlParams);
let zoom = 7;
let centreLatLng = [-79, -137]
if (urlZoom) {
zoom = urlZoom;
}
if (urlCentreX && urlCentreY && urlCentreZ) {
const centrePos = new Position(Number(urlCentreX), Number(urlCentreY), Number(urlCentreZ));
centreLatLng = centrePos.toLatLng(map);
} else if (urlRegionID) {
const region = new Region(Number(urlRegionID));
const centrePos = region.toCentrePosition()
centreLatLng = centrePos.toLatLng(map);
zoom = urlZoom || 9;
}
map.setView(centreLatLng, zoom)
});