Importing GeoJSON data into Meteor's Leaflet

Recently diving into Meteor, I am on a mission to create my own customized version of this impressive example from leaflet incorporated into Meteor: Interactive Choropleth Map

The implementation requires the use of this GeoJson Data file: us-states

The obstacle before me is: dealing with the import and rendering of this file.

My Approach:

Template.map.rendered = function() {

  var map = L.map('map').setView([37.8, -96], 5);

  L.tileLayer.provider('Stamen.Watercolor').addTo(map);
  
  HTTP.get(Meteor.absoluteUrl("/us-states.js"), function(err,result) {
    var statesData = result.content;

    console.log(statesData);

    var myStyle = {
        "fillColor": "#487ba1",
        "weight": 3,
        "opacity": 1,
    "color": "#487ba1",
    "fillOpacity": 0.1
    };
    
    var statesLayer = L.geoJson(statesData,  {
        style: myStyle
      }).addTo(map);
  });
  
}
#map {
  width: 100%;
  height: 100%;
}
<div id="column">
  {{> map}}
</div>
<template name="map">
  <div id='map'></div>
</template>

The Outcome: Uncaught Error: Invalid GeoJSON object.


The variable "stateData" seems to be returning as an object. My objective here is plain - display the GeoJson data on the map. This would usually be easy on traditional html. What aspect might I be missing in getting this to operate smoothly?

Answer №1

The issue appears to be originating from your us-states.js file. The intention is to load data from it, but instead of data, there is a variable declaration within it:

var statesData = {
  "type":"FeatureCollection",
  "features": [
    ....
  ]
}

To resolve this, you should retain only the data in your file and remove the var statesData = section:

{
  "type":"FeatureCollection",
  "features": [
    ....
  ]
}

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 steps can be taken to restrict a user's access to a route through URL parameters?

I am currently developing an Express application that generates and exports a document based on the selections made by a user from two dropdown menus. The values selected in the dropdowns are passed as route parameters after the user clicks a button, trigg ...

What is the process to insert a record into a table by triggering an AJAX call upon clicking "save

I'm looking to dynamically update a table with data from a database using AJAX. Specifically, I want the table to reflect any new records added by the user without having to refresh the entire page. Below is my JavaScript code snippet for handling thi ...

Unlock the capability to automatically swipe on a React Native TextInput Carousel while the user types

My challenge involves setting up a carousel with either Flatlist or ScrollView (I have tested both options). This Carousel consists of multiple TextInputs. The goal is to achieve the following: There are 4 TextInputs. When the user enters 6 digits in the ...

Angular2: Issue encountered while processing click event

When I click a button on my client application, it sends a request to the server I created using Express. The request handler in the server simply logs 'Delete from server' every time the button is clicked. I am encountering these errors when cl ...

The JSON data retrieved from the server through Unity is empty

I am facing an issue where the Json data retrieved from the server is always showing as NULL. Feel free to test out the code, the URL is public. Here is the Json data received from the server: [{"id":"3","question":"Cik ir 4 + 4 = ?","answer1":"3","answ ...

The ng-model does not reflect changes when using the JQuery datepicker

Currently, I have set up two textboxes for users to choose a date. I am utilizing JqQuery's datepicker UI to showcase a small calendar pop-up whenever the user clicks on the textbox. However, an issue arises when I select a date in the calendar popup ...

Searching in MongoDB using periods

Hey, I'm running into an issue with the full text search feature in MongoDB. Let me give you an example: db.test.insert({fname:"blah.dll"}) db.test.insert({fname:"something.dll"}) db.test.ensureIndex({fname:"text"}) After setting this up, if I run.. ...

Is it possible to distinguish and identify each separate window when a user opens multiple instances of your site?

Has anyone explored the possibility of distinguishing between multiple windows a user may open on the same site? My goal is to assign the initial window the name "window1" and subsequent windows "window2" and so forth. Is there a method for achieving this ...

Can you guide me on how to programmatically set an option in an Angular 5 Material Select dropdown (mat-select) using typescript code?

I am currently working on implementing an Angular 5 Material Data Table with two filter options. One filter is a text input, while the other is a dropdown selection to filter based on a specific field value. The dropdown is implemented using a "mat-select" ...

Storing information in a database using Phantomjs

My app is built on phantomjs and here's how it currently operates: 1. A php script retrieves data from my postgres database as an array, 2. The array of data is then passed as an argument to a shell_exec command running a phantomjs script, 3. Phantomj ...

Refresh the view when the URL is modified

Utilizing angularjs alongside ui-router (using the helper stateHelperProvider) to organize views and controllers on the page. Encountering an issue where the views are not updating as expected. The relevant code snippet config.js app.config(function($h ...

Inject parameters into the templateUrl property of an Angular component

In this scenario, my component object is structured as follows: var options = { bindings: { title: '<', rows: '<' }, controller: registers, templateUrl: function ($element, $attrs) { return ...

What is the best way to structure a JSON response in python code?

Here is a sample of my JSON data: data = [{"document_id":"FT_3860001798686","party_type":"1","name":"LEE, GABRIEL"},{"document_id":"FT_3860001798686","party_type":&qu ...

Why does JSON remain unchanged when a value is explicitly assigned in Javascript

Why isn't my JSON structure updating when I explicitly assign a new value? items[0][i]['human_addressItem'] = address; I am trying to reverse geocode the latitude and longitude to obtain the human address, which is working fine. However, I ...

The current date object in JavaScript will only display the year or a combination of the month and

$scope.articles = [ { link: "http://google.com", source: "Google", title: "hello", "date": new Date(2008, 4, 15) }, ]; <tbody> <tr ng-repeat = "article in articles | orderBy:sortType:sortReverse | filter:searchArticle ...

The list marquee in HTML, JS, and CSS is not being properly rendered on

I recently made changes to a code that showcases a marquee scrolling a basic HTML list. Check it out here: I am facing 3 issues that I am struggling to resolve: JS: I want the marquee to continuously show text re-entering from the right just after it ...

The images in the React slick carousel appear to be slightly out of

I'm experiencing blurriness in my carousel when it animates with card items. Despite searching for a solution, I haven't found one yet. My tech stack includes Next.js and TypeScript. const ref = useRef<any>(); const settings = { arro ...

Tips for positioning the labels in a sankey diagram for optimal alignment

When multiple values are the same in this scenario, such as 0, the labels start to overlap. Can anyone provide guidance on how to align these labels vertically? Ideally, I would like them to be positioned at the top of the node/bar. Highcharts.chart(&apos ...

Streamline jQuery code for dynamically populating two select dropdowns

I am looking to streamline and enhance this script to make it more dynamic. There could be multiple items in options, potentially even up to ten items. In the current scenario, the maximum number of items allowed is 2. The total value selected across both ...

Why is there a discrepancy between the value displayed in a console.log on keydown and the value assigned to an object?

As I type into a text box and log the keydown event in Chrome, I notice that it has various properties (specifically, I'm interested in accessing KeyboardEvent.code). Console Log Output { altKey: false bubbles: true cancelBubble: false cancelable: t ...