Code Monkey home page Code Monkey logo

leaflet-maps-open-data-apis's Introduction

leaflet-maps-open-data-apis

Leaflet map with multiple API data feeds Socrata and Esri ArcGIS Online. View demo.

Screenshot

If you plan to query Socrata heavily, it is recommended you sign up for an API token. See more details at https://dev.socrata.com/.

Example of data fetch from Socrata

The original example showed hospital locations in North Dakota provided by Medicare.gov website. This example was modified on 23 March 2022 due to Medicare.gov replacing Socrata with a different database system. In this updated example, AmeriCorps NCCC projects in North Dakota are shown.

Assuming you have your map initialized and layers control created, load data from a JSON endpoint in Socrata and add it to the map, using custom icons for markers.

    $.getJSON("https://data.americorps.gov/resource/yie5-ur4v.json?stabbr=ND", function(data) {

      // Array of markers
      var markers = [];
      
      // For each row in Socrata, create a marker
      for (var i = 0; i < data.length; i++) {
        
        var item = data[i];
    
        // Extract coordinates, convert strings to floats
        var coordinates = [
          parseFloat(item.geocoded_column.latitude),
          parseFloat(item.geocoded_column.longitude)
        ]

        // Create a marker with a custom icon
        var marker = L.marker(coordinates, {
            icon: L.icon({
              iconUrl: 'images/americorps.png',
              iconSize: [24, 24],
              iconAnchor: [12, 12],
              opacity: 0.5
            })
        }).bindTooltip(item.sponsor + '<br>' + item.project_description);

        // Add marker to the array of markers
        markers.push(marker);
      }

      // Create a Leaflet layer group from array of markers
      var layer = L.layerGroup(markers);
      layer.addTo(map); // add layer to the map

      // Add layer to the legend, together with the little icon
      legend.addOverlay(layer, 'AmeriCorps NCCC <img src="images/americorps.png" height="11" alt="AmeriCorps NCCC">')

    })

Example of data load using esri-leaflet plugin

Esri-leaflet plugin allows you to load the data without using jQuery's getJSON() function. You can add point-level data to the map as shown below:

var ems = L.esri.featureLayer({
  url: 'https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Emergency_Medical_Service_(EMS)_Stations_gdb/FeatureServer/0/',
  where: "STATE = 'ND'",
  pointToLayer: function(feature, latlng) {
    return L.circleMarker(latlng, {
      radius: 4,
      fillColor: 'blue',
      color: 'blue',
      weight: 0.1,
      opacity: 1,
      fillOpacity: 0.5,
      pane: 'markerPane'  // to make sure points stay above polygons and remain clickable
    }).bindTooltip(feature.properties.NAME)
  }
}).addTo(map)

Learn more

Credits

  • Leaflet (see license)
  • Esri Leaflet plugin (Apache 2.0)
  • jQuery (MIT)

leaflet-maps-open-data-apis's People

Contributors

ilyankou avatar jackdougherty avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.