Using Leaflet to make geojson XMLHttpRequests

Recently, I've been exploring the world of JavaScript and attempting to map around 2,200 data points in Leaflet. Despite following a helpful tutorial on pulling data from a geojson file and displaying it on a map, I can't seem to make it work with my own data. I've tried different hosting sources and even experimented with test data, but I'm still stuck on where the issue lies - whether it's the host or the geojson file itself.

Below is the code I've been working with (using test data and icon files from the tutorial). If someone could take a look at it and provide insights on why the data isn't loading onto my map, I would greatly appreciate it! Any suggestions or tips on what steps I should take next would be really helpful. My background in coding is primarily with R, so there may be some fundamental aspects that are eluding me.

<html>
<head>
  <title>A Leaflet map!</title>
  <link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="076b6266616b6273473629372934">[email protected]</a>/dist/leaflet.css" />
  <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="442821252228213004756a746a77">[email protected]</a>/dist/leaflet.js"></script>
  <script src="https://raw.githubusercontent.com/leaflet-extras/leaflet-providers/master/leaflet-providers.js"></script>
  <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  <style>
    #map{ height: 900px;width: 650px }
  </style>
</head>
<body>

  <div id="map"></div>

  <script>

var map = L.map('map').setView([-41.291, -185.229], 6);

var OpenMapSurfer_Roads = L.tileLayer('http://korona.geog.uni-heidelberg.de/tiles/roads/x={x}&y={y}&z={z}', {
    maxZoom: 20,
    attribution: 'Imagery from <a href="http://giscience.uni-hd.de/">GIScience Research Group @ University of Heidelberg</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);


$.getJSON("https://bitbucket.org/whalebiologist/website-map/raw/58abf2f24696fef437c294c02e55019d1c6554e4/churches_short.geojson",function(data){
  var ratIcon = L.icon({
    iconUrl: 'http://maptimeboston.github.io/leaflet-intro/rat.png',
    iconSize: [60,50]
  });
  L.geoJson(data,{
    pointToLayer: function(feature,latlng){
  var marker = L.marker(latlng,{icon: ratIcon});
  marker.bindPopup(feature.properties.Location + '<br/>' + feature.properties.OPEN_DT);
  return marker;
}
  }).addTo(map);
});

  </script>
</body>
</html>

A big thank you to anyone willing to assist with this predicament!

Answer №1

Welcome to Stack Overflow!

If you encounter issues with HTML and JavaScript, a useful tool for debugging is your browser's console, such as Chrome's console.

Upon loading your page, check the console for any error messages. I've noticed an error related to mime types for leaflet-providers.js. To resolve this, convert the URL using rawgit.com. For more information, refer to this link.

The updated script source can be found at .

Furthermore, it seems that the $.getJSON function is not triggering the success callback, indicating a possible request error. The use of jQuery's getJSON also offers a Promise feature, allowing us to rearrange the code to troubleshoot potential errors. Take a look at The jqXHR Object for more details.

$.getJSON("https://bitbucket.org/whalebiologist/website-map/raw/58abf2f24696fef437c294c02e55019d1c6554e4/churches_short.geojson")
    .then(function (data) {
        var ratIcon = L.icon({
            iconUrl: 'http://maptimeboston.github.io/leaflet-intro/rat.png',
            iconSize: [60, 50]
        });
        L.geoJson(data, {
            pointToLayer: function (feature, latlng) {
                var marker = L.marker(latlng, { icon: ratIcon });
                marker.bindPopup(feature.properties.Location + '<br/>' + feature.properties.OPEN_DT);
                return marker;
            }
        }).addTo(map);
    })
    .fail(function(err){
        console.log(err.responseText)
    });

Upon reviewing the fail() response, we noticed that the geojson file seems to be restricted by a login requirement on bitbucket.

To address this issue, consider moving the geojson file out of a password-protected area.

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

Implementing a dynamic like functionality using Ajax in Django

I've created a new structure for the like button, but it's not functioning correctly. Here are the files I'm working with: models.py class Comment(models.Model): title = models.CharField(max_length=50) author = models.ForeignKey(Pr ...

The illumination in three.js failed to display properly when viewed on Chrome through an Apache server

this illustration shows the issue at hand const directionalLight = new THREE.DirectionalLight(0xffffff, 0.65, 0); directionalLight.position.set(100, -50, 200); scene.add(directionalLight); const ambientLight = new THREE.AmbientLight(0xfff5f3); ...

Utilizing modal dialogs in HTML and navigating back to the previous page

I'm currently learning about HTML, CSS, JavaScript, and SQL. Here is some context: Imagine a scenario where I am developing a website to manage information on dogs, their owners, and the relationships between them. All this data is stored in an SQL ...

What is the fastest way to retrieve the total count of documents in a view in Lotus Domino?

Is there a way to accurately retrieve the total count of documents in a view using Ajax? Currently, I am obtaining the value through h t t p://.../viewname?readviewentries but I have come across an issue where the "toplevelentries" attribute remains static ...

Utilizing cross-domain AJAX to broaden the range of requests sent to an API service

Is there a way for users to directly access data from the openFDA's new API using a client-side web app? The API can be found at Given the daily request limit of 1000, with 60000 requests allowed with a key, could users send requests to the FDA' ...

Solving template strings in a future context

I have a unique use-case scenario where I am filling the innerHTML like this. However, my issue lies in resolving the template literal within the context of a for loop. Any suggestions on how to accomplish this? var blog_entries_dom = 'blog_entries& ...

What is the best way to implement KaTeX in a React application?

Can someone help explain how KaTex can be incorporated into a React application? Despite reviewing the documentation on NPM, I am still having trouble understanding. I am specifically confused on how katex.render and katex.renderToString functions can be ...

Vue - unable to display component CSS classes in application when using class-style bindings

Just diving into Vue and starting with class-style binding syntax. I'm facing an issue where the CSS classes for header and footer that I've defined are not displaying, even though I've referenced them in the component tags. Can't seem ...

When constructing a file directory URL, it is important to utilize the forward slash "/" in the filename

As I am creating a URL, the process involves taking the filename and using it to create a folder with that name. However, an issue arises if the name contains "/", as it causes the URL to break and creates an undesired location. For example: var fileDir ...

`"Type is invalid" error occurring with component after importing it into a different project``

I am currently working on developing a custom Storybook 7 Typescript component library with React. I have successfully imported this library into another project using a private NPM package. However, one of the components in the library, specifically the ...

What is the best way to consistently position a particular row at the bottom of the table in AG-grid?

I have successfully implemented a table using AG-grid. Here is the code snippet: .HTML <ag-grid-angular #agGrid style="width: 100%; height: 100%; font-size: 12px;" class="ag-theme-alpine" [rowData]=&quo ...

Encountering the error message "Uncaught Error: Objects are not valid as a React child" even though I am not passing objects as children in my React component

My current challenge involves mapping an array of objects, which I retrieved from an axios.get request and then passing them as children to React components. The error message that's causing trouble for me reads as follows: An Error occurred: Objects ...

MERN Stack deployment to Heroku: Remote rejected - unable to push changes to master branch (pre-receive hook declined)

Just a heads up: I've successfully deployed using this method around 3 times in the past, but now it seems to be failing. Could there have been an update with Heroku that's causing this issue? Not entirely sure... I'm attempting to push my ...

Implementing AJAX to dynamically insert content into div elements on the page

Currently facing a small issue with my AJAX implementation for creating comments on posts. The functionality is working well, but the problem arises when executing it in the index.html.erb view. The create.js.erb file locates the initial div labeled "comme ...

Using PHP Escape Functions in JavaScript

Can anyone help me with displaying item descriptions retrieved from a database query? Here is the code snippet that should display the description along with other details... <?php $type = "item"; $limit = 16; $preparedStatement = $SQL->prepare(& ...

Sending the "Enter Key" using JavaScript in Selenium can be achieved by utilizing the "executeScript" function

I'm currently developing an automation flow using IE 11 with Selenium and Java. On a particular web page, I need to input a value in a Text Box and then press Enter. I have successfully managed to input the values using the following code: // The &ap ...

Tips for confirming a date format within an Angular application

Recently, I've been diving into the world of Angular Validations to ensure pattern matching and field requirements are met in my projects. Despite finding numerous resources online on how to implement this feature, I've encountered some challenge ...

Information is not appearing in the table

I'm having trouble displaying data in a table format. The issue arises when I try to fetch data from a JSON file using a custom service. The fetched data is then inserted into the $rootScope object. However, when I preview the view, it appears blank ...

What could be causing my array to be undefined upon the creation of a Vue component?

I'm struggling to comprehend why my array is showing as undefined in my Vue component. The issue arises in the following scenario: The SelectCategories.vue component uses the router to navigate to the Quiz.vue component. Here, I utilize props to tran ...

Retrieve the highest integer from the server-side for the client

When creating input fields of type number on the client side, I have been setting their max attribute to the maximum integer value in the user's browser using: Number.MAX_SAFE_INTEGER.toString() Now, I need to output an input field on the se ...