"Upon refreshing the browser, an Angular map loses its positioning and appears blank

I have implemented a LeafLet map in my Laravel 9 system, which is functioning properly with some exceptions.

Here's how it looks like: https://i.stack.imgur.com/kcuVr.jpg

The code in the controller (CompetitionsMapController.php) is as follows:

    {
        $data = Competition::all();
        return view('map', ['data'=> $data]);
    }

This is the link in the navigation blade:

<li ng-class="{'active': (currentRoute=='competition.map')}">
     <a ui-sref="map">{{_('MAP')}}</a>
 </li>

And here is what's defined in the public/js/app.js:

    $stateProvider
    .state('map', {
        url: '/map',
        parent: 'root',

        views: {
            'content@':  {
                templateUrl: 'map'
            },       
        },
        restricted: true
    });

Everything works fine until I refresh the browser, then the map disappears.

As seen here: https://i.stack.imgur.com/OMM68.jpg

To make the map display again, I have to select another option on the navigation bar and come back to the MAP section. Even though Google Map works well after a browser refresh, this issue occurs only with the LeafLet map. Interestingly, if I include a simple JavaScript file within the application, it functions even after a refresh, suggesting that the problem lies specifically with the LeafLet map script.

Here is the LeafLet map script causing the issue:

var map = L.map('map').setView([57.36,15.26], 7);
          mapLink = '<a href="http://openstreetmap.org">OpenStreetMap</a>';
          L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '&copy; ' + mapLink + ' Contributors',
            maxZoom: 18,
            }).addTo(map);

So, I'm wondering what might be causing the problem and how can it be fixed?

While analyzing the error, I identified an issue in Safari:

Error: Can't find variable: L
eval code@
eval@[native code]
globalEval@http://127.0.0.1:8000/js/vendor.js:1:14516
M@http://127.0.0.1:8000/js/vendor.js:1:4621
append@http://127.0.0.1:8000/js/vendor.js:2:22024
@http://127.0.0.1:8000/js/vendor.js:2:23130
$e@http://127.0.0.1:8000/js/vendor.js:2:9577
html@http://127.0.0.1:8000/js/vendor.js:2:22802
@http://127.0.0.1:8000/js/vendor.js:22:14027
ye@http://127.0.0.1:8000/js/vendor.js:5:795
f@http://127.0.0.1:8000/js/vendor.js:4:25187
o@http://127.0.0.1:8000/js/vendor.js:4:20911
@http://127.0.0.1:8000/js/vendor.js:4:20442
c@http://127.0.0.1:8000/js/vendor.js:22:13517
@http://127.0.0.1:8000/js/vendor.js:22:13786
$broadcast@http://127.0.0.1:8000/js/vendor.js:6:4545
@http://127.0.0.1:8000/js/vendor.js:22:9167
o@http://127.0.0.1:8000/js/vendor.js:5:27411
@http://127.0.0.1:8000/js/vendor.js:5:27583
$digest@http://127.0.0.1:8000/js/vendor.js:6:1689
$apply@http://127.0.0.1:8000/js/vendor.js:6:3444
u@http://127.0.0.1:8000/js/vendor.js:5:9841
b@http://127.0.0.1:8000/js/vendor.js:5:11764
@http://127.0.0.1:8000/js/vendor.js:5:12237 – "<div ui-view=\"content\" class=\"ng-scope\" data-ng-animate=\"1\">"

Answer №1

Issue resolved! After selecting the map from the navigation, an additional blade called app.blade.php is loaded. By adding the following code snippet:

<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed838a8e89c38382939ca38b848a8487">[email protected]</a>/dist/leaflet.js"></script>

The map now updates correctly. This simple fix resolves the problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to divide an array into pairs and store them in separate arrays?

I'm attempting to challenge my JavaScript skills and faced with a dilemma. There is an array containing data var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];. The goal is to pair the elements and generate a new array of arrays such as var newArray = [[1 ...

How can I retrieve the current user in Angular with Firebase authentication?

Struggling with building an app using ionic v2 & firebase v3. Trying to access the logged-in user's details has been a challenge. In my app.ts, I successfully connect to my firebaseapp and recognize the logged-in user, leading them to the homepage: ...

Having trouble sending an array from Flask to a JavaScript function

As a newcomer to web development and JavaScript, I'm struggling to pass an array from a Flask function into a JavaScript function. Here's what my JS function looks like: function up(deptcity) { console.log('hi'); $.aja ...

Include characteristics in JSX.Element following its declaration

Suppose I have an item in a dictionary with the following structure: let item = { element: <myElement/>, color: "#0e76a8" } Is it possible to add a style attribute to the item.element within the render() method? I tried the following appro ...

Is there a more efficient approach to streamline Javascript code similar to the chaining method used in JQuery?

When working with jQuery, it's possible to streamline the code by running multiple methods in a single statement: $("#p1").css("color", "red").html("Hello world!").attr("class","democlass"); But how can this be accomplished in Javascript? document. ...

Navigating the world of updating problems with Express.js

Currently, I am working on a MEAN stack project and facing challenges with the routing for updating. Saving operations are functioning correctly. In my Angular controller, I have defined the following scope method in which staff represents the object subm ...

Rxjs: Making recursive HTTP requests with a condition-based approach

To obtain a list of records, I use the following command to retrieve a set number of records. For example, in the code snippet below, it fetches 100 records by passing the pageIndex value and increasing it with each request to get the next 100 records: thi ...

Material UI AppBar displaying custom colors instead of theme colors

Currently, I am utilizing Material UI version 4.7.0. In my project, I have established a customized theme (shown below) by utilizing createMuiTheme() method with personalized primary and secondary colors set. Within my AppBar component (also displayed be ...

Problem arising from apostrophe usage in Javascript OData request

In my current project, I have a text input field that passes a value through JS to fetch filtered data of names from a JSON file using OData query parameters. However, I've encountered an issue where if a name contains an apostrophe, it results in a ...

Is it possible to have scope inherit from an object in AngularJS?

Imagine creating an app like this: <script> myArray=<?php echo $array;?>; app={ myArray:myArray, myIndex:myArray.length-1, back:function(){this.myIndex--;console.log("You clicked back");}, forward:function(){this.myIndex++} } ...

Dropdown Pattern with React CTA Modal

While using MaterialUI's ButtonGroup for a dropdown menu, I encountered an issue trying to set up a series of CTAs that are easily interchangeable within it. The goal is to have all components reusable and the choices in the dropdown dynamic. const C ...

Arranging array elements by both date and alphabetical order using Javascript

Is there a way to sort the data by both date and alphabet at the same time? Alphabetical order seems fine, but the date sorting isn't working correctly. Thank you for any solutions. Data structure : [{ productId: 21, title: "Huawei P40 L ...

Is Node.js going to continue to provide support for its previous versions of node modules?

I currently have a website that relies on the following dependencies. While everything is working smoothly at the moment, I can't help but wonder about the future support of these packages by node.js. I've looked into the legacy documentation of ...

TinyMCE - The control with the name 'content' is considered invalid and cannot receive focus

I am utilizing TinyMCE 4 and here is the code snippet for it: <script type="text/javascript" src="//cdn.tinymce.com/4/tinymce.min.js"></script> <script> tinymce.init({ selector: 'textarea[name=content]', ...

How can we store data coming from PHP using AJAX and update the color of a div whenever new data is inserted?

Hey there, I'm currently working on a project where I need to save values and display them using Ajax after inserting them into a MySQL table using PHP. However, I'm having trouble with the alert function not working as expected. Let me share my ...

Transform a list of file directories into a JSON format using ReactJS

I am developing a function that can convert a list of paths into an object structure as illustrated below; this script will generate the desired output. Expected Output data = [ { "type": "folder", "name": "dir1& ...

Using Angular JS to submit forms on a regular basis

My HTML form is set up within an Angular controller with inputs, action, and other elements already defined. The only issue I'm facing is that the form does not have a traditional submit button. Instead, there is a separate button on the page outside ...

Initiate an AJAX call

Hey there, I have a piece of code that I need some help with. <button onclick="sbt()" name="step1[save]" type="submit" class="btn-type5 next-btn-form pie" value="Далее">Send</button> function sbt(){ var phone = document.getElementById(&ap ...

Having trouble retrieving Bengali-language data from the server using jQuery AJAX

I am facing an issue where I am unable to fetch data in Bengali language from the server using ajax. Strangely, the data retrieved from the server is getting replaced by some unknown characters. However, if I directly retrieve the data without using ajax, ...

Tips for resolving the issue of the symbol ' displaying as &#39 in an Angular 2 application

I am currently working on an Angular application that makes API calls when a name displayed in a grid table is clicked. However, I have encountered an issue where names containing an apostrophe are being displayed incorrectly as &#39 instead. I managed ...