I am attempting to port an enterprise webforms app to blazor server. Mapping is what I'm currently stuck on. I am trying to import the Leaflet js library but i can't get it and it's dependencies to all resolve. We use mapping in a modal that sits in the (previously MasterPage) MainLayout.razor file. An input type image triggers the modal to appear with the maps.
I have a function that initiates the map
private IJSObjectReference Map;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
Map = await Script.InvokeAsync<IJSObjectReference>("import", "./scripts/MyMap.js");
await Map.InvokeVoidAsync("ShowMap", some, str, parms);
}
}
That block calls the intial function which adds map markers (custom tooltips that contain user input and a lot of other things). Within that function is where things go awry.
import * as L from './leaflet/leaflet-src.esm.js';
import './leaflet/markercluster-src.js'
import './leaflet/esri-leaflet-vector.'
import './leaflet/esri-leaflet-vector.js';
export function ShowMap(some, rand, str) {
var wayPoints = {};
var popup = L.DomUtil.get('mappin');
if (popup != null)
popup._leaflet_id = null;
var lat = '39.8283';
var lng = '98.5795';
var map = L.map('mappin', { center: [lat, lng], zoom: 10 });
var markers = new L.MarkerClusterGroup();
var markersList = [];
// request public vector tile layer from ArcGIS Online
L.esri.Vector.vectorTileLayer("http://arcgiswebsitethatwegetvectortilesfrom.lmao", {
// add service to map
}).addTo(map);
map._layersMaxZoom = 19;
map._layersMinZoom = 2;
//ommitted code that does the custom stuff
map.addLayer(markers);
}
The problem is that trying to import leaflet I have to use the leaflet esm and import * as L to get the L object, but then everything else that relies on L will fail during the import because L won't have a definition for this or that, all because I am using the leaflet-src.esm.js file. Markercluster and Esri Vector both rely on L and markercluster doesn't have a module that I can find. I tried dissecting the npm leaflet.markercluster.esm package and it 'worked' but then I got stuck on the Esri leaflet package needing the L reference as well. I'm new to the js reference world. I'm used to being able to throw a script tag at the top of the page and moving on with life. Blazor makes this a lot more difficult. Does anyone have any ideas? End of the day I'm trying to import Leaflet js, markercluster js, esri-vector js and have them all work together OnAfterRenderAsync.