{"id":36657,"date":"2016-06-27T13:21:33","date_gmt":"2016-06-27T07:51:33","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=36657"},"modified":"2016-06-27T13:21:33","modified_gmt":"2016-06-27T07:51:33","slug":"getting-started-with-leaflet-maps","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/getting-started-with-leaflet-maps\/","title":{"rendered":"Getting started with Leaflet maps"},"content":{"rendered":"<p>Leaflet is a open source Javascript library that can be used to create interactive maps. It comes\u00a0with all\u00a0mapping features that most of us need in any project like adding markers, \u00a0popups, vector layers, zooming, selecting lat-lng of a place and much\u00a0more.<\/p>\n<p>In this blog, we will learn how to get started with Leaflet maps and we will also do some stuffs like adding markers\u00a0and\u00a0popups.<\/p>\n<p><strong>Setting up your page<\/strong><\/p>\n<ul>\n<li>In the head section of your html page include\u00a0Leaflet CSS file.\n<p>[code language=&#8221;javascript&#8221;]<br \/>\n       &lt;link rel=&quot;stylesheet&quot; href=&quot;http:\/\/cdn.leafletjs.com\/leaflet\/v0.7.7\/leaflet.css&quot; \/&gt;<br \/>\n      [\/code]<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>Include\u00a0Leaflet \u00a0JavaScript file.\n<p>[code language=&#8221;javascript&#8221;]<br \/>\n      &lt;script src=&quot;http:\/\/cdn.leafletjs.com\/leaflet\/v0.7.7\/leaflet.js&quot;&gt;&lt;\/script&gt;<br \/>\n      [\/code]<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>Add\u00a0div Element with some Id in which you want to render your map.\n<p>[code language=&#8221;javascript&#8221;]<br \/>\n      &lt;div id=&quot;myMap&quot;&gt;&lt;\/div&gt;<br \/>\n      [\/code]<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>Make sure your\u00a0map container has a defined height. You can use CSS like this:\n<p>[code language=&#8221;javascript&#8221;]<br \/>\n      #myMap { height: 250px; }<br \/>\n      [\/code]<\/p>\n<\/li>\n<\/ul>\n<p>Now, let&#8217;s start with initializing the map.<\/p>\n<p><strong>Initializing the map<\/strong><\/p>\n<p>First, initialize your map and set its view to some geographical coordinates and a zoom level.<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\nvar map = L.map(&#8216;myMap&#8217;).setView([51.505, -0.09], 13);\/\/\u00a0center of London with zoom level 13[\/code]<\/p>\n<p>Next, add a tile layer to map, in this case OpenStreetMap tile layer. Adding a tile layer involves setting the URL template for the tile images and the attribtution :<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\nL.tileLayer(&#8216;http:\/\/{s}.tile.osm.org\/{z}\/{x}\/{y}.png&#8217;, {<br \/>\n    attribution: &#8216;&amp;copy; &lt;a href=&quot;http:\/\/osm.org\/copyright&quot;&gt;OpenStreetMap&lt;\/a&gt; contributors&#8217;<br \/>\n}).addTo(map);<br \/>\n[\/code]<\/p>\n<p>Make sure all these lines of code is\u00a0called\u00a0after the div and the javascript file leaflet.js inclusion. Now, map will get rendered inside your map container.<\/p>\n<p><strong>Adding marker<\/strong><\/p>\n<p>Marker can easily be added by setting lat-lng.\u00a0Let\u2019s add a marker:<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\n     var marker = L.marker([51.5, -0.09]).addTo(map);<br \/>\n      [\/code]<\/p>\n<p><strong>Adding popups<\/strong><\/p>\n<p>Popups are used when you want to add some additional information with an object on the map. Attaching popup to objects is very simple in Leaflet:<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\n    marker.bindPopup(&quot;&lt;strong&gt;Hello world!&lt;\/strong&gt;&lt;br&gt;I am a popup.&quot;).openPopup();<br \/>\n      [\/code]<\/p>\n<p>After, doing all the above stuffs your final map will look something like this:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-36753\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/leaftlet.png\" alt=\"map\" width=\"1045\" height=\"401\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Leaflet is a open source Javascript library that can be used to create interactive maps. It comes\u00a0with all\u00a0mapping features that most of us need in any project like adding markers, \u00a0popups, vector layers, zooming, selecting lat-lng of a place and much\u00a0more. In this blog, we will learn how to get started with Leaflet maps and [&hellip;]<\/p>\n","protected":false},"author":229,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[1],"tags":[3616,3614,3615,3626],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36657"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/229"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=36657"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36657\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=36657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=36657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=36657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}