So I wanted to use leaflet, but the apps for Wix were all google maps. So I thought lets try to implement it. This is the first part for using leaflet. Later on I will use it’s possibilities to plot gpx files.
In this case I have a database table which contains information for the overview. The datacollection has the columns ‘marker_id’, ‘marker_description’, ‘marker_long’, ‘marker_lat’. I won’t give my exact example for it’s also taking into consideration language based description columns.
After making the datacollection I made a backend file. There is still one part not correct in it, so the code I’ll fix later on. This is working however.
//file: backend/vttLeaflet.jsw
import wixData from 'wix-data';
export function getOverviewPoints() {
// this is temporary for the query isn't working yet
let oMarkers = [{
"marker_description": "CWU",
"marker_id": "en_cwu",
"marker_lat": "56.824421",
"marker_long": "-5.125890"
},{
"marker_description": "DC2C",
"marker_id": "en_dc2c",
"marker_lat": "50.317583",
"marker_long": "-4.084997"
}];
// This is not working yet
wixData.query("markers")
.find()
.then((oMarkers) => {
for (const oMarker in oMarkers) {
this.oMarkers.push({
"marker_id": oMarker.marker_id
});
}
});
return oMarkers;
}
Then I made a page ‘Overview’. On it I placed an HTML element and I called it ‘htmlOverview’. To populate the markers, I added some page code
import {getOverviewPoints} from 'backend/vttLeaflet';
import {session} from 'wix-storage';
$w.onReady(function () {
setTimeout(function(){
getOverviewPoints().then(oMarkers => {
$w("#htmlOverview").postMessage(oMarkers);
});
}, 1000);
});
This code will get the markers from the backend and then pass them to the html element placed on the page. Obviously you need to do something with that data. You want to display a world map with the markers on it. So you need to add code to the html element.
EDIT: slowdown the code
I came to the conclusion, that the preview was fine but if you navigated away and back to the page, it didn’t generate the make. I have had similar issue with other languages so I tried the trick adding a small timeout. That took care of this.
<!DOCTYPE html>
<html style="height: 100%; width: 100%">
<head>
<link rel="stylesheet"
href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css"
integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"
integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log=="
crossorigin="">
</script>
<style>
body {
background-color: transparent;
width: 100%;
height: 100%;
overflow: hidden;
margin: 0;
}
#vttMapOverview {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="vttMapOverview"></div>
<script type="text/javascript">
// When data received
window.onmessage = (event) => {
if (event.data) {
// initialize map
let mapOverview = L.map('vttMapOverview', {
zoom: 13
});
// Add tilelayer and define layout of map
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: 'Some text you want to put here (bottom right of the map)'
}).addTo(mapOverview);
// Add markers
const OMarkers = event.data;
let markerCollection = L.featureGroup();
for (const OMarker in OMarkers) {
const marker = L.marker(
[OMarkers[OMarker]["marker_lat"],OMarkers[OMarker]["marker_long"]], {
title: OMarkers[OMarker]["marker_description"]
}
).addTo(mapOverview);
markerCollection.addLayer(marker);
}
mapOverview.fitBounds(markerCollection.getBounds());
}
};
</script>
</body>
</html>
This code is added to the html element. It will process the data passed from the ‘postMessage’ and place a marker for each entry then it will zoom to fit the map to all the markers. Hovering over the marker will give the description. Off course there are many more options. I will still do:
- custom marker icons to illustrate status
- click function to open the related page
- change the tile layer (great collection can be found here)
When I figured out my back-end code I correct it here too. Oh, the result will be something like this. For those wondering, it’s going to be a map displaying trail runs I did/will do