Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use topoJSON in Leaflet map

I'm learning how to use Leaflet to make online interactive maps for public health purposes (experienced ArcGIS user, Mapbox TileMill). I'm taking it slow so I understand each piece of code, and I'm working from the Leaflet choropleth example as I want to make choropleth maps. The current task I'm stuck on is how to correctly add topoJSON data to a Leaflet map. I've tried the following code to convert the us states geoJSON to topoJSON, but it hasn't worked. Any suggestions?

    var geojson;

    var test = topojson.feature(us-states-topo, us-states-topo.objects.layer1 );

    geojson = L.geoJson(test, {
        style: style,
        onEachFeature: onEachFeature
    }).addTo(map);

I've reviewed the topoJSON API reference, but I'm sure I must be making a simple error as I am a beginner to JavaScript in general. Thank you all for your help!

Best Eli

like image 824
Eli Kern Avatar asked Nov 21 '25 18:11

Eli Kern


1 Answers

I'd recommend using your browser debug tools to start through debugging this.

var test = topojson.feature(us-states-topo, us-states-topo.objects.layer1 );

This is not valid JavaScript: us-states-topo is not a valid variable name, since -s are not permitted.

like image 90
tmcw Avatar answered Nov 24 '25 22:11

tmcw