-
Notifications
You must be signed in to change notification settings - Fork 2
/
009script.js
180 lines (156 loc) · 5.03 KB
/
009script.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const initialZoom = 13
const latitude = 58.146
const longitude = 7.998
// Map
var map = L.map("map").setView([latitude, longitude], initialZoom);
// OsmTileLayer
const openStreetMapTileLayerLink = "https://tile.openstreetmap.org/{z}/{x}/{y}.png";
const openStreetMapAttribution = '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors';
// OtmTileLayer
const openTopoMapLayerLink = "https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png";
const openTopoMapMaxZoom = 17;
const openTopoMapAttribution = 'Map data: © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: © <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)';
// Norkart API key
const apiKey = "" // User your own key from https://developer.norkart.no/
/**************************
* Defining the map Layers *
**************************/
var openStreetMap = L.tileLayer(openStreetMapTileLayerLink,
{
attribution: openStreetMapAttribution,
}
);
var openTopoMap = L.tileLayer(openTopoMapLayerLink,
{
maxZoom: openTopoMapMaxZoom,
attribution: openTopoMapAttribution,
}
);
var norkartLite = L.tileLayer.webatlas(
{
mapType: L.TileLayer.Webatlas.Type.LITE,
apikey: apiKey,
}
);
var norkartHybrid = L.tileLayer.webatlas(
{
mapType: L.TileLayer.Webatlas.Type.HYBRID,
apikey: apiKey,
}
);
var norkartAerial = L.tileLayer.webatlas(
{
mapType: L.TileLayer.Webatlas.Type.AERIAL,
apikey: apiKey,
}
);
/** All available Norkart map types
L.TileLayer.Webatlas.Type.GREY
L.TileLayer.Webatlas.Type.VECTOR
L.TileLayer.Webatlas.Type.MEDIUM
L.TileLayer.Webatlas.Type.LITE
L.TileLayer.Webatlas.Type.AERIAL
L.TileLayer.Webatlas.Type.HYBRID
*/
/***********************************
* Async henting av geojson *
***********************************/
async function getGeojson(path) {
return fetch(path).then((response) => response.json());
}
/***********************************
* Setting up a marker with a popup *
***********************************/
const myMarkersGEOJSON = await getGeojson("data/myMarkers.geojson");
var myMarkers = L.geoJSON(myMarkersGEOJSON, {
onEachFeature: function (feature, layer) {
layer.bindPopup(
"<h4>" + feature.properties.name + "</h4>" +
"<p>" + feature.properties.popupContent + "</p>"
);
},
}).addTo(map);
const barsInNorwayGEOJSON = await getGeojson("data/barsNorway.geojson");
var markersBarsInNorway = L.geoJSON(barsInNorwayGEOJSON, {
onEachFeature: function (feature, layer) {
var popupcontent = [];
for (var prop in feature.properties) {
if(prop != "@id"){
popupcontent.push(prop + ": " + feature.properties[prop]);
}
}
layer.bindPopup(popupcontent.join("<br />"));
},
}).addTo(map);
/****************
* Adding circle *
****************/
const myPointGEOJSON = await getGeojson("data/myPoint.geojson");
var circles = L.featureGroup();
L.geoJSON(myPointGEOJSON, {
onEachFeature: function (feature, layer) {
var latlng = feature.geometry.coordinates;
L.circle([latlng[1], latlng[0]],
{
radius: feature.properties.radius,
}
).addTo(circles);
}
});
circles.addTo(map);
/*************************
* Adding line with popup *
*************************/
const uiaDistanceGEOJSON = await getGeojson("data/universityUiA.geojson");
var uiaDistance = L.geoJSON(uiaDistanceGEOJSON, {
onEachFeature: function(feature, layer) {
var distance = feature.geometry.coordinates
var latlng1 = L.latLng(distance[0][1], distance[0][0]);
var latlng2 = L.latLng(distance[1][1], distance[1][0]);
var distanceInMeters = Math.round(latlng1.distanceTo(latlng2));
layer.bindPopup(
"<h4>"+ feature.properties.description +"</h4>" +
"<p>Distance: " + distanceInMeters + "m</p>"
)
}
}).addTo(map);
const uiaKrsGEOJSON = await getGeojson("data/uiaKrsPolygon.geojson");
var uiaKrs = L.geoJSON(uiaKrsGEOJSON).addTo(map);
/***********************
* Adding a draw plugin *
***********************/
var drawItems = new L.FeatureGroup();
map.addLayer(drawItems);
var drawControl = new L.Control.Draw({
edit: {
featureGroup: drawItems,
},
});
map.addControl(drawControl);
map.on("draw:created", function (event) {
var layer = event.layer,
feature = (layer.feature = layer.feature || {});
feature.type = feature.type || "feature";
var props = (feature.properties = feature.properties || {});
drawItems.addLayer(layer);
});
/***************************************
* Setting up the map and adding layers *
***************************************/
norkartAerial.addTo(map);
var baseLayers = {
OpenStreetMap: openStreetMap,
Topo: openTopoMap,
NorkartLite: norkartLite,
NorkartHybrid: norkartHybrid,
NorkartAerial: norkartAerial,
};
var overlays = {
Markers: myMarkers,
BarsInNorway: markersBarsInNorway,
Circles: circles,
UiADistance: uiaDistance,
UiAKrsPoly: uiaKrs,
DrawnElements: drawItems,
};
L.control.layers(baseLayers, overlays).addTo(map);