You define your L property on the window after the DOMContentLoaded event has been fired. The code in your script tag is getting executed right away when you access your page (before DOMContentLoaded fires). Is there a reason that you assign your map related variables to your window in the DOMContentLoaded callback? Because it shouldn't be necessary.
Jun 5, 2023
2
Level 63
Leaflet : Uncaught ReferenceError: L is not defined
Hello,
I'm searching for some hours now to find the reason why leaflet doesn't work.
It's an application with Laravel 10 and ViteJS.
Here is my app.js.
import './bootstrap';
import Alpine from 'alpinejs'
window.Alpine = Alpine;
Alpine.start();
import { L } from 'leaflet';
import { GeoSearchControl, OpenStreetMapProvider } from 'leaflet-geosearch';
window.addEventListener('DOMContentLoaded', event => {
window.L = L;
window.OpenStreetMapProvider = OpenStreetMapProvider;
window.GeoSearchControl = GeoSearchControl;
});
And here the view.
<script>
var latitude = '{{ $player->latitude }}';
var longitude = '{{ $player->longitude }}';
var map = L.map('map').setView([latitude, longitude], 6)
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var icon = new L.Icon.Default();
icon.options.shadowSize = [0,0];
L.marker([latitude, longitude], {icon: icon})
.addTo(map)
.bindPopup('{{ $player->localization_string }}')
.openPopup();
</script>
And I get this error.
Uncaught ReferenceError: L is not defined
If you have any advice for me, it would be great.
Thanks a lot ;).
V
Please or to participate in this conversation.