{"id":195,"date":"2024-02-25T17:22:52","date_gmt":"2024-02-25T17:22:52","guid":{"rendered":"https:\/\/www.acugis.com\/gis-tutorials\/?page_id=195"},"modified":"2024-02-25T17:22:52","modified_gmt":"2024-02-25T17:22:52","slug":"leaflet-with-geojson-layer-and-features","status":"publish","type":"page","link":"https:\/\/www.acugis.com\/gis-tutorials\/web-mapping-with-leafletjs\/leaflet-with-geojson-layer-and-features\/","title":{"rendered":"Leaflet with GeoJSON Layer and Features"},"content":{"rendered":"\n<p>In our previous tutorial, we created a <a href=\"https:\/\/www.acugis.com\/gis-tutorials\/web-mapping-with-leafletjs\/leaflet-with-geojson-layer\/\" data-type=\"page\" data-id=\"154\" target=\"_blank\" rel=\"noreferrer noopener\">Leaflet map with a GeoJSON layer<\/a>.<\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll use jQuery to add add pop-up feature information to our map.<\/p>\n\n\n\n<p>As with the previous tutorial, we&#8217;ll cover using both a GeoJSON file as well as GeoJSON from a GeoServer url.<\/p>\n\n\n\n<p>Let&#8217;s start with our basic Leaflet map with GeoJSON layer that we created earlier:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n&lt;head>\n    &lt;base target=\"_top\">\n    &lt;meta charset=\"utf-8\">\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n    \n    &lt;title>Leaflet GeoJSON Example&lt;\/title>\n    \n    &lt;!-- Leafletjs and CSS -->    \n    &lt;script src=\"https:\/\/unpkg.com\/leaflet@1.9.4\/dist\/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2\/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\">&lt;\/script>\n    \n    &lt;link rel=\"stylesheet\" href=\"https:\/\/unpkg.com\/leaflet@1.9.4\/dist\/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD\/miZyoHS5obTRR9BMY=\" crossorigin=\"\"\/>\n\n    &lt;style>\n        html, body {\n            height: 100%;\n            margin: 0;\n        }\n        .leaflet-container {\n            height: 80%;\n            width: 80%;\n            max-width: 100%;\n            max-height: 100%;\n        }\n    &lt;\/style>\n\n    &lt;\/head>\n&lt;body>\n\n    &lt;div id=\"map\">&lt;\/div>\n   \n    &lt;script>\n\n    \/\/ Add OpenStreetMap basemap\n        var map = L.map('map').setView([41.8916, -87.7219], 4);\n\n        var osm = L.tileLayer('https:\/\/tile.openstreetmap.org\/{z}\/{x}\/{y}.png', {\n            maxZoom: 19,\n            attribution: '&amp;copy; &lt;a href=\"http:\/\/www.openstreetmap.org\/copyright\">OpenStreetMap&lt;\/a>'\n        }).addTo(map);\n\n     \n       \/*Add JSON location or url*\/\n\n       var url = 'states.json';  \/\/ path to json file\n\n    \t\/*fetch JSON data and to map*\/\n\n       fetch(url)\n       .then(function(response) {\n          return response.json()\n       })\n       .then(function(data) {\n         L.geoJson(data).addTo(map);\n       })  \n\n&lt;\/script>\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<p>We now want to make our layer display feature data on click.<\/p>\n\n\n\n<p>First, we need to add the jQuery library to the &lt;head> section of our file.<\/p>\n\n\n\n<p>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.7.1.js\" integrity=\"sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=\" crossorigin=\"anonymous\">&lt;\/script>\n<\/pre>\n\n\n\n<p>Next, we need to add some jQuery to get our data.<\/p>\n\n\n\n<p>Each section is commented below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\n\/\/Loop through JSON file for features information -->\n\tfunction forEachFeature(feature, layer) {\n    \n\t\/\/ Print all feature information\n    var mypopupContent = '&lt;pre>'+JSON.stringify(feature.properties,null,' ').replace(\/[\\{\\}\"]\/g,'')+'&lt;\/pre>';\n    \n\t\/\/ above Bind to popup content\n    layer.bindPopup(mypopupContent);\n            \n\t\t}\n\t\n    \/\/ Null var that will hold layer\n\tvar myLayer = L.geoJson(null, {onEachFeature: forEachFeature});\n\n    \/\/ Add mylayer above to map\n\t$.getJSON(url, function(data) {\n   \tmyLayer.addData(data);\n\t});\n\tmyLayer.addTo(map);<\/pre>\n\n\n\n<p>With our jQuery in place, our html code now looks like below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n&lt;head>\n    &lt;base target=\"_top\">\n    &lt;meta charset=\"utf-8\">\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n    \n    &lt;title>Leaflet GeoJSON Example&lt;\/title>\n    \n    &lt;!-- Leafletjs and CSS -->    \n    &lt;script src=\"https:\/\/unpkg.com\/leaflet@1.9.4\/dist\/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2\/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\">&lt;\/script>\n    \n    &lt;link rel=\"stylesheet\" href=\"https:\/\/unpkg.com\/leaflet@1.9.4\/dist\/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD\/miZyoHS5obTRR9BMY=\" crossorigin=\"\"\/>\n\t\n\t&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.7.1.js\" integrity=\"sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=\" crossorigin=\"anonymous\">&lt;\/script>\n\n    &lt;!-- Inline map styling -->\n    \n    &lt;style>\n        html, body {\n            height: 100%;\n            margin: 0;\n        }\n        .leaflet-container {\n            height: 80%;\n            width: 80%;\n            max-width: 100%;\n            max-height: 100%;\n        }\n    &lt;\/style>\n\n    \n&lt;\/head>\n&lt;body>\n\n    &lt;div id=\"map\">&lt;\/div>\n\n\n&lt;script>\n\t\n\n\tvar map = L.map('map').setView([47.7541, -107.05078], 3); \n\n\tvar osm = new L.tileLayer('https:\/\/{s}.tile.osm.org\/{z}\/{x}\/{y}.png',{ \n\t\t\t\tattribution: '&amp;copy; &lt;a href=\"http:\/\/osm.org\/copyright\">OpenStreetMap&lt;\/a> contributors'}).addTo(map);\n\t\n\tvar url = 'states.json';  \/\/ GeoJSON data source. Can be file or GeoServer url.\n\t\t\n\t\/\/Loop through JSON file for features information -->\n\tfunction forEachFeature(feature, layer) {\n    \n\t\/\/ Print all feature information\n    var mypopupContent = '&lt;pre>'+JSON.stringify(feature.properties,null,' ').replace(\/[\\{\\}\"]\/g,'')+'&lt;\/pre>';\n    \n\t\/\/ above Bind to popup content\n    layer.bindPopup(mypopupContent);\n            \n\t\t}\n\t\n    \/\/ Null var that will hold layer\n\tvar myLayer = L.geoJson(null, {onEachFeature: forEachFeature});\n\n    \/\/ Add mylayer above to map\n\t$.getJSON(url, function(data) {\n   \tmyLayer.addData(data);\n\t});\n\tmyLayer.addTo(map);\n\n&lt;\/script>\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<p>Now, when we click on our map, the data for the feature is returned.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"698\" src=\"https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/geojson-data-1024x698.png\" alt=\"\" class=\"wp-image-202\" srcset=\"https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/geojson-data-1024x698.png 1024w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/geojson-data-300x204.png 300w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/geojson-data-768x523.png 768w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/geojson-data.png 1096w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In the code above, we are looping through all of the features and displaying them.<\/p>\n\n\n\n<p>You can limit this by substituting individual features.<\/p>\n\n\n\n<p>We can replace line below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var popupContent = '&lt;pre>'+JSON.stringify(feature.properties,null,' ').replace(\/[\\{\\}\"]\/g,'')+'&lt;\/pre>';<\/pre>\n\n\n\n<p>With this, which returns only the State name:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var popupContent = \"State Name: \" + feature.properties.STATE_NAME;\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Note the url that is generated:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">https:\/\/yourdomain.com\/geoserver\/topp\/ows?service=WFS&amp;version=1.0.0&amp;request=GetFeature&amp;typeName=topp:states&amp;maxFeatures=50&amp;outputFormat=application\/json<\/pre>\n\n\n\n<p>Now, paste the url in place of &#8216;states.json&#8217; in your code as below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> var url = 'https:\/\/yourdomain.com\/geoserver\/topp\/ows?service=WFS&amp;version=1.0.0&amp;request=GetFeature&amp;typeName=topp:states&amp;maxFeatures=50&amp;outputFormat=application\/json';  \/\/ path to json file<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our previous tutorial, we created a Leaflet map with a GeoJSON layer. In this tutorial, we&#8217;ll use jQuery to add add pop-up feature information to our map. As with the previous tutorial, we&#8217;ll cover using both a GeoJSON file as well as GeoJSON from a GeoServer url. Let&#8217;s start with our basic Leaflet map &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.acugis.com\/gis-tutorials\/web-mapping-with-leafletjs\/leaflet-with-geojson-layer-and-features\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Leaflet with GeoJSON Layer and Features&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":166,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-195","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":9,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/195\/revisions"}],"predecessor-version":[{"id":205,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/195\/revisions\/205"}],"up":[{"embeddable":true,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/166"}],"wp:attachment":[{"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/media?parent=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}