9951 explained code solutions for 126 technologies


javascript-d3How do I create a map with d3.js and GeoJSON?


To create a map with d3.js and GeoJSON, you will need to include the d3.js library in your project, and create a GeoJSON object containing the geographic data you would like to display.

You can then use the d3.js library to parse the GeoJSON data and create a map. Here is an example code block:

//Create a new d3.js SVG element
var svg = d3.select("body").append("svg")

//Define a geographic projection
var projection = d3.geoMercator()

//Create a geographic path generator
var path = d3.geoPath()
    .projection(projection)

//Create a GeoJSON object
var geojson = {
    type: "FeatureCollection",
    features: [
        {
            type: "Feature",
            geometry: {
                type: "Point",
                coordinates: [-122.3321, 47.6062]
            },
            properties: {
                title: "Seattle"
            }
        }
    ]
}

//Bind the data to the SVG
svg.selectAll("path")
    .data(geojson.features)
    .enter()
    .append("path")
    .attr("d", path)

This code block will create an SVG element, define a geographic projection, create a geographic path generator, create a GeoJSON object, and bind the GeoJSON data to the SVG. The output of this code will be an SVG element with a path representing the GeoJSON data.

Code explanation

  1. Create a new d3.js SVG element: var svg = d3.select("body").append("svg")
  2. Define a geographic projection: var projection = d3.geoMercator()
  3. Create a geographic path generator: var path = d3.geoPath().projection(projection)
  4. Create a GeoJSON object: var geojson = { ... }
  5. Bind the data to the SVG: svg.selectAll("path").data(geojson.features).enter().append("path").attr("d", path)

Helpful links

Edit this code on GitHub