Customized coordinates on leaflet map fail to render

Utilizing the Where the ISS at API to retrieve the current latitude and longitude coordinates of the ISS involves making a serverside request structured like this:

app.get("/coordinates", async (req,res) =>{
    try{
    const result = await axios.get(API_URL);
    const latitude = result.data.latitude;
    const longitude = result.data.longitude;
    res.render("index.ejs", ({latitude: latitude, longitude: longitude}))
    } catch (error) {
    res.status(404).send(error.message);
        
    }
})

The EJS template includes the following code snippet:

  <!-- leaflet css  -->
    <link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="254940444349405165140b120b14">[email protected]</a>/dist/leaflet.css" />

</head>

<body>
    <div id="map"></div>
    <p>ISS Latitude: <span id="data-latitude"><%= latitude %></span> </p>
    <p>ISS Longitude: <span id="data-longitude"> <%= longitude %></span></p>

    <!-- leaflet js  -->
    <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="264a4347404a4352661708110817">[email protected]</a>/dist/leaflet.js"></script>
    <!-- Your custom JavaScript file -->
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

    <script src="/js/main.js"></script>
</body>

</html>

And the client-side JavaScript appears as follows:

const mapElement = document.getElementById('map');
const latitude = parseFloat(document.getElementById("data-latitude").textContent);
const longitude = parseFloat(document.getElementById("data-longitude").textContent);

// Map initialization with predetermined coordinates
var map = L.map(mapElement).setView([latitude, longitude], 8);

// osm layer
var osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
});
osm.addTo(map);

// Marker at predetermined coordinates
var marker = L.marker([latitude, longitude]).addTo(map);

Upon loading the specific route on my localhost, a text snippet reading "Copy Code" appears with the coordinates displayed underneath. While the coordinates are successfully retrieved, the map fails to render. As a beginner with leaflet, I am unsure of the issue causing this problem.

Answer №1

<link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e38f8682858f8697a3d2cdd4cdd2">[email protected]</a>/dist/leaflet.css" />
ISS Latitude: 39.90° ISS Longitude: 116.41°
<!-- leaflet js  -->
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9b5bcb8bfb5bcad99e8f7eef7e8">[email protected]</a>/dist/leaflet.js"></script>
<!-- Your custom JavaScript file -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

<script src="/js/main.js"></script>

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

Animate sliding bar to move from the left using jQuery

I am currently experiencing an issue with a sliding animation on mouseover in the navigation. The animation works fine, but the problem arises when I hover over any menu item - the sliding bar starts from the very left instead of starting where the navigat ...

Ajax is able to fetch a JSON string, however it struggles to iterate through the successful data as an

My current project involves creating a graph using d3.js. I came across some php code that fits my needs perfectly. However, I am working with c# and Visual Studio, so I need to convert it into asp.net. For starters, I want to input some hardcoded sample d ...

The invokeApply parameter within $interval remains unchanged and has no effect

In the documentation for AngularJS's $interval service, it is mentioned: invokeApply (optional) boolean: If set to false, skips model dirty checking. Otherwise, will invoke the function within the $apply block. This implies that setting invokeApp ...

Determine the Total of Various Input Numbers Using JQuery

There are 3 input fields where users can enter numbers, and I want to calculate the sum of these numbers every time a user updates one of them. HTML <input class="my-input" type="number" name="" value="0" min="0"> <input class="my-input" type="n ...

Activate or deactivate a button depending on the input value of a TextField in React.js

I am currently designing a form similar to the one displayed in this image. I want the "Add" button to become active as soon as the user starts typing in the "Tags" input field. For this project, I am using material-ui and I have created an Input compone ...

Model not found in Node.js on Express framework

I am facing an error in my router file, where I have a post method that instantiates an object from the Contact model. However, I am receiving undefined for the Contact model and the error message is as follows: Error: **TypeError: Cannot read property & ...

Eslint problem: no-duplicates Fixing issue: cannot load resolver "node"

After performing an update on my project (a SPA using VueJS and Quasar Framework) today with npm update, I am encountering difficulties running it. An error message no-duplicates Resolve error: unable to load resolver "node" keeps appearing in various mod ...

Using Cocoon Gem in Rails 5 to create nested forms within nested forms

Currently, I am experimenting with the cocoon gem to construct nested forms efficiently. Within my application, I have defined models for Organisation, Package::Bip, and Tenor. The relationships between these models are as follows: Organisation has_ma ...

Iterating over an object using ng-repeat in Angular, where the value is an array

In my data object, I have key-value pairs where the value is an array. Each array contains objects with various properties. $scope.testObj = { "London":[ {"id":1,"city":"London","country":"GB","name":"Test1"}, {"id":4,"city":"London" ...

Mastering Typescript lookup types - effectively limit the properties included in a merge operation with the Partial type

Exploring lookup types, I'm interested in creating a safe-merge utility function that can update an entity of type T with a subset of keys from another object. The objective is to leverage the TypeScript compiler to catch any misspelled properties or ...

What is the best way to transform this JSON data into an array of key-value pairs in JavaScript?

Dealing with nested JSON data can be challenging, especially when trying to extract key-value pairs efficiently. If anyone has suggestions on how to simplify this process and improve readability, please share your insights. The goal is to transform the ne ...

Having trouble loading script files with JSDOM in NodeJS?

I'm currently experimenting with loading an HTML page in jsdom to generate graphs, but I'm facing challenges in getting the JavaScript to execute. Here's the HTML page I'm trying to load, which simply renders a basic graph: <html&g ...

Perform the cloning process for each item listed in my JSON file

I'm trying to display a list of names from a JSON file on my webpage, each in their own separate div. However, I've been unsuccessful in getting it to work so far. Currently, this is what I have attempted: var cloneTeam = $('.team').c ...

Displaying the Status of a Script that is Running Asynchronously

My script takes around 5 minutes to complete and sends an email with a file attachment once finished. The entire process happens on a single PHP page without using AJAX. I want the front end to handle form submission seamlessly, processing the request in ...

Having trouble getting the Angular 2 quickstart demo to function properly?

Just starting out with Angular 2, I decided to kick things off by downloading the Quickstart project from the official website. However, upon running it, I encountered the following error in the console: GET http://localhost:3000/node_modules/@angular/ ...

An error popped up while using the fetch API due to an unexpected end of input

fetch('http://www.freegamesforyourwebsite.com/feeds/games/?tag=platform&limit=100&format=json', { method:'GET', mode:'no-cors', dataType: 'json', headers: { 'Accept': 'a ...

What distinguishes loading angular dependencies and what method is optimal for module sharing?

Can you explain the distinction between these two methods of loading modules? angular.module('CoreApp', ['...','...','...','...']); //parent module angular.module('MainApp1', ['CoreApp' ...

"Error: Trying to access form data in Node Express 3.4.4 with Jade returns

The code below is part of the app.use section // Setting up redis session store app.use(express.cookieParser()); app.use(express.session({ store: new redisStore({ host: 'localhost', port: 6379, ...

The 'IN' Operator in JavaScript

Recently, I embarked on a journey to learn the art of JavaScript, and my current project involves creating a Tic Tac Toe game. However, I've hit a roadblock with the IN statement, as it consistently returns True under the specified condition. functio ...

Menu is not functioning properly as it is not staying fixed in place

I am trying to create a fixed menu that sticks to the browser window as it scrolls. However, I am encountering an issue where the transition from sticky to fixed is not smooth when I remove position: relative; from navbar__box. window.onscroll = functio ...