Plot data points from geojson onto a leaflet map using markers

How can I efficiently import geoJson data (containing over 2000 coordinates) into a leaflet map? Below is a brief snippet of geo json:

{
   "type": "FeatureCollection",
   "features": [
       {
           "type": "Feature",
           "geometry": {
               "type": "Point",
               "coordinates":  [ 44.8242557024,20.4048512901 ]
           },
           "properties": {}
       },
       {
           "type": "Feature",
           "geometry": {
               "type": "Point",
               "coordinates":  [ 44.8242557024,20.4048512901 ]
           },
           "properties": {}
       },...
    ]
}

The code snippet I have attempted is as follows:

<!doctype html>
<html>
<head>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
    <!--[if lte IE 8]>
        <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.ie.css" />
    <![endif]-->
    <style type="text/css">
        body {
            padding: 0;
            margin: 0;
        }

        html, body, #cmap {
            height: 100%;
        }
    </style>
    <script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
    <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
</head>
<body>
    <div id="cmap"></div>
    <script>
        var cupcakeTiles = L.tileLayer('https://api.mapbox.com/v4/mapbox.emerald/page.html?access_token=cj5h2mqa63sc92srx3bu62e2j', {
            maxZoom: 18
        });

    $.getJSON("convertcsv.geojson", function(data) {
        var geojson = L.geoJson(data, {
            onEachFeature: function (feature, layer) {
                layer.bindPopup(feature.properties.name);cj5h2mqa63sc92srx3bu62e2j
          }
      });

    var map = L.map('map', {
        center: [44, 20],
        zoom: 7
    });

    L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {       
        id: 'examples.map-20v6611k'
    }).addTo(map);

    new L.GeoJSON(meta1nJson).addTo(map);
  });
  </script>
</body>
</html>

However, when I run the code, nothing happens and only a gray background is displayed. I am uncertain about the mistake I may have made (or errors), but suspect there could be an issue with importing geojson data and the map token. I am fairly new to this so any help in advance would be greatly appreciated.

Answer №1

Your code appears to have several issues that need to be addressed. Firstly, it seems like the element with the id 'map' is not present in your HTML, causing the map layer placement to fail. You should use 'cmap' as the id in the provided code snippet.

var map = L.map('cmap', {
   center: [44, 20],
   zoom: 7
});

Additionally, the variable meta1nJson is not defined in your code, making the following code ineffective:

new L.GeoJSON(meta1nJson).addTo(map);

Even though the layer 'cupcakeTiles' is declared, it is never added to the map. Furthermore, there is an extra string in the code snippet below that needs to be removed.

 $.getJSON("convertcsv.geojson", function(data) {
var geojson = L.geoJson(data, {
  onEachFeature: function (feature, layer) {
    layer.bindPopup(feature.properties.name); //cj5h2mqa63sc92srx3bu62e2j
  }
});

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 could be causing my for loop to become unresponsive?

My for loop seems to be populating all fields with the last object parsed. http://codepen.io/anon/pen/EKxNaN This is my current code. I created something similar on CodePen since I can't access JSON from the original source there. var championMaste ...

Having trouble retrieving a dynamic name with Formcontrol error?

I keep encountering a typeError in this section of code <p *ngIf="formValue.controls['{{obj.name}}'].invalid, but when I manually enter it like this *ngIf="formValue.controls['uname'].invalid it works perfectly fine. What ...

Grab the last item from jq output (not an array)

Looking to extract the last item labeled as "date" from the data provided? The desired result should be: "2019_10_29_12_01_01" $ cat snapshots.json | jq '.snapshots[] | select (.state == "SUCCESS") | {date: .snapshot}' { "date": "2019_10_21_1 ...

Upon clicking, Vue triggers form validation in Laravel

I have encountered an issue that I am unable to solve by myself: Within my Laravel view, I have included a form in the following manner {!! Form::open(array('route' => ['reports.store', $project->id], 'data-parsley-validate& ...

When importing a module, the function in the ts file may not be recognized or located

While attempting to create a VSTS (Azure Devops) Extension, I encountered a perplexing issue. Within my HTML page, I have a button element with an onclick listener: <!DOCTYPE html> <head> <script type="text/javascript"> VS ...

What are the steps to implementing PNG masking using PixiJS?

I am currently working on incorporating a png sprite as a mask for another layer. I found a demo on the pixi.js official website and created this fiddle: https://jsfiddle.net/raphadko/ukc1rwrc/ The code snippet below is what I am using for the masking fu ...

Guide on separating a variable by commas using jQuery

After making an Ajax call to a Java servlet, I am retrieving data and storing it in the 'data' variable upon success. Here is the code snippet: var s1=""; var ticks =""; $('#view').click(function(evt ...

Protractor Error: Identifier Unexpectedly Not Found

I've encountered a slight issue with importing and exporting files while working on Protractor tests. HomePage.js export default class HomePage { constructor() { this.path = 'http://automationpractice.com/index.php'; this.searchQ ...

What is the best way to correctly render several React components using a single index.js file?

I am facing an issue with rendering two React component classes. One class creates a counter and works fine, while the other class generates a simple string wrapped in HTML tags but does not render. I have tried various tutorials to troubleshoot this probl ...

Instead of presenting MySQL data in tables on an HTML page, showcase it in editable text boxes

I have successfully imported data from my database table into an HTML table, but now I want to display them in locked text boxes. When the user clicks an "edit" button, the corresponding text box should become unlocked so that they can edit the data and sa ...

Unique calculation for rotational movement

I am currently developing a unique compass application. Although the project is progressing well, I am facing a significant challenge with one aspect: My code calculates degree angles within the range of -360 and 360: -318°, -29°, 223°, -163°, ... ...

Optimal method for incorporating Django with an Ajax framework

When it comes to integrating javascript libraries with a Django application, the options are abundant and diverse. Personally, I am leaning towards utilizing jQuery due to its popularity and robust features, although I am open to exploring other alternativ ...

Utilizing mapped data to display numerous Material-UI Dialog elements

On my table, I have a list of users displayed. Each user has a button in their row to delete them. Clicking the delete button triggers a Material-UI Dialog to confirm. An issue arises where 3 dialogs are being rendered due to mapping, and the last dialog ...

Divide information in the chart

<table id="tab"> <tr><td class="sub">mmmmm</td><td class="sub">jjjjj</td></tr> <tr><td class="sub">lllll</td><td class="sub">wwwww&l ...

javascriptcode to execute before loading google maps

I am facing an issue with displaying markers on Google Maps. The problem arises when the array with latitude and longitude values is constructed through an Ajax request. Due to the map being loaded before the initialization of the array, I am unable to see ...

Transform the styles from a React component into Cascading Style Sheets

I have been tasked with transitioning all the styles from the JavaScript file to the CSS file while ensuring the design remains intact. I am facing an issue where using the CSS styles breaks the search field design. The original design can be seen here: S ...

Developing a vue.js component library without the need for constant rebuilding after every edit

Introduction: I have created two projects using vue-cli ~4.2.0: parent-app - the main project dummylib - a library that is imported by parent-app. It contains several .vue components. Currently, parent-app functions well in dev mode with dummylib being ...

I am currently experiencing difficulties with jQuery version 1.7.2 when passing an AJAX params object with JSONP

Can someone help me with this script? I am trying to send service, data and content parameters but it's not working as expected. var addons = {}; $.ajax({ 'url': 'http://site.com/index.php', dataType:'jsonp' ...

Node(Meteor) experiencing a memory leak due to setTimeout

I have encountered an unusual memory leak associated with the use of setTimeout. Every 15 seconds, I execute the following code using an async function that returns an array of promises (Promise.all). The code is supposed to run again 15 seconds after all ...

Is it possible to include a while loop for iteration inside a AJAX success function?

I'm currently working on dynamically creating an accordion and populating the content of each tab using AJAX. In my main HTML page, there is an empty div reserved for the accordion: <div id="accordion"></div> The desired outcome is to ha ...