{"id":154,"date":"2024-02-25T08:09:08","date_gmt":"2024-02-25T08:09:08","guid":{"rendered":"https:\/\/www.acugis.com\/gis-tutorials\/?page_id=154"},"modified":"2024-02-25T08:09:08","modified_gmt":"2024-02-25T08:09:08","slug":"leaflet-with-geojson-layer","status":"publish","type":"page","link":"https:\/\/www.acugis.com\/gis-tutorials\/web-mapping-with-leafletjs\/leaflet-with-geojson-layer\/","title":{"rendered":"Leaflet with GeoJSON Layer"},"content":{"rendered":"\n<p>This post will cover creating a basic Leaflet map using a GeoJSON data source.<\/p>\n\n\n\n<p>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 Leaflet map skeleton:<\/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\t&lt;base target=\"_top\">\n\t&lt;meta charset=\"utf-8\">\n\t&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\t\n\t&lt;title>Leafletjs Map&lt;\/title>\n\t\n\t&lt;!-- Leafletjs and CSS -->    \n\t&lt;script src=\"https:\/\/unpkg.com\/leaflet@1.9.4\/dist\/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2\/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\">&lt;\/script>\n\t\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/unpkg.com\/leaflet@1.9.4\/dist\/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD\/miZyoHS5obTRR9BMY=\" crossorigin=\"\"\/>\n\n\t\n\t\n\t&lt;style>\n\t\thtml, body {\n\t\t\theight: 100%;\n\t\t\tmargin: 0;\n\t\t}\n\t\t.leaflet-container {\n\t\t\theight: 80%;\n\t\t\twidth: 80%;\n\t\t\tmax-width: 100%;\n\t\t\tmax-height: 100%;\n\t\t}\n\t&lt;\/style>\n\n\t\n&lt;\/head>\n&lt;body>\n\n\t&lt;div id=\"map\">&lt;\/div>\n\n\n\t\n\t&lt;script>\n\t\t\n\t\/\/ Add OpenStreetMap basemap\n\t\tconst map = L.map('map').setView([41.8916, -87.7219], 4);\n\n\t\tconst osm = L.tileLayer('https:\/\/tile.openstreetmap.org\/{z}\/{x}\/{y}.png', {\n\t\t\tmaxZoom: 19,\n\t\t\tattribution: '&amp;copy; &lt;a href=\"http:\/\/www.openstreetmap.org\/copyright\">OpenStreetMap&lt;\/a>'\n\t\t}).addTo(map);\n\n\t\n\t\n\n\n\n&lt;\/script>\n\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<p>We have a local file, states.json.  <\/p>\n\n\n\n<p>We now add some javascript to fetch our json data and add it to the map<\/p>\n\n\n\n<p>The first section sets the data location and the second adds the data to our map.<\/p>\n\n\n\n<p>Add the code below just above the closing &lt;\/script> tag<\/p>\n\n\n\n<p>Note: this code is intended for use on a server, if run locally you will get a CORS exception.<\/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=\"\">\n\/*Add JSON location or url*\/\n       var url = 'states.json';  \/\/ path to json file\n       \n\/*fetch JSON data and to mao*\/\n       fetch(url)\n       .then(function(response) {\n          return response.json()\n       })\n       .then(function(data) {\n         L.geoJson(data).addTo(map);\n       })  <\/pre>\n\n\n\n<p>So our final code 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\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>If we open the map in our browser, it now looks like below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"613\" src=\"https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/firtmap-1024x613.png\" alt=\"\" class=\"wp-image-160\" srcset=\"https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/firtmap-1024x613.png 1024w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/firtmap-300x180.png 300w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/firtmap-768x460.png 768w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/firtmap.png 1032w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Using a GeoServer url for JSON data.<\/p>\n\n\n\n<p>If we wish to, we can use a GeoServer JSON url for our JSON data source.<\/p>\n\n\n\n<p>In order to do so, we will need to enable CORS for our GeoServer instance.<\/p>\n\n\n\n<p>To get our GeoJSON url from GeoServer, go to Layer Preview page and select GeoJSON from the Formats dropdown for the layer you would like to use.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"337\" src=\"https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/getjson-geoserver.png\" alt=\"\" class=\"wp-image-163\" srcset=\"https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/getjson-geoserver.png 782w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/getjson-geoserver-300x129.png 300w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/getjson-geoserver-768x331.png 768w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\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>Refresh the page and your map should look the same as previous, but is now using your GeoServer GeoJSON url.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post will cover creating a basic Leaflet map using a GeoJSON data source. We&#8217;ll cover using both a GeoJSON file as well as GeoJSON from a GeoServer url. Let&#8217;s start with our Leaflet map skeleton: We have a local file, states.json. We now add some javascript to fetch our json data and add it &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.acugis.com\/gis-tutorials\/web-mapping-with-leafletjs\/leaflet-with-geojson-layer\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Leaflet with GeoJSON Layer&#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-154","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/154","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=154"}],"version-history":[{"count":9,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/154\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/154\/revisions\/165"}],"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=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}