Syntax Video
FREE maps for any app - replace Google maps TODAY
- 0:00 intro
- 1:48 how do map libraries work?
- 4:17 client js - per load fees
- 6:15 tile server - usage fees
- 7:45 free map libraries
- 8:18 maplibre short history - mapbox fork
- 9:15 free map tile servers
- 9:54 what is openstreetmap?
- 10:47 openfreemap is good
- 11:00 vector tile services
- 11:18 vector tile specification
- 11:37 map styles
- 14:20 code examples
- 14:41 vanilla example
- 16:39 vanilla leaflet example
- 18:08 maplibre framework adapters
- 19:41 react example
- 22:03 vue example
- 24:25 svelte example
- 26:18 awesome-maplibre
- 26:36 thanks
intro
if you have ever tried to add a custom map to your website like this one you know that the process is very tricky right you have to find potentially a library that's going to work for you and then you have to look at their pricing model and if you're not careful you might end up with an $111,000 per day Bill well in this video I'm going to show you how to get a map like this on your website completely for free using open free map now open free map is two things first of all they have a free public tile server now we'll talk about what tile servers are and why you need them but their tile server is completely free to use there's no usage fees there's no API keys they don't even store cookies you can use it completely for free even for commercial purposes Now hypernut who is the creator of this project also wanted to make it really easy for anyone to host their own tile servers as well so they have the free public instance but if you really want to host it on your own servers maybe you want to host it on an internet or something like that they have a GitHub repo with everything you need to get up and going with your own tile server if you really want to now this tile server works really great if you combine it
with map Libre so map Libre is a completely open- Source library for adding maps to your web applications and it was actually forked from mapbox whenever mapbox changed their license but you can use open free map tiles directly inside of your map lib applications now I have plenty of code examples that I've prepared for you that will show you how to add open free map and map libr to your application so if you want to just get right to the code click the link in the description check out the repo and I have examples in vanilla JS react view felt and then also show how to integrate with leaflet as well which is another free map library now I'm not just going to show you code examples I'm also going to talk about what open free map is how it can exist and I'll also talk about a short history of map libraries in the world of JavaScript and how we got to where we are today so if that sounds good to you let's dive in my name is CJ welcome to syntax now first of all I want to talk about how these map libraries actually work and so when you visit a webp page that has an interactive map on it like this the first thing that needs to happen is the browser is going to load a
how do map libraries work?
JavaScript live library that is actually used to display the map and so for this example I have a react application and it's using map Libre and also the vigl rapper library of map Libra but you'll see in the network tab here that it loaded those files in but because these packages can be installed from mpm I didn't have to hit any external server these are actually just loaded from the V Dev server that's serving up the application so that's step one it loaded in the JavaScript and now we can actually have an interactive map now step two is this library now needs to request tiles or vectors based on what the user is viewing and those tiles and vectors are requested from a map tile server so you'll see here as I navigate around if I reach a part of the map or a zoom level that I have not seen before some requests get fired off and these are requests for the actual vectors that describe these various parts of the map that I'm looking at you can see all these requests being fired off but if I hover over them you can see the request is being made to tiles. openen free.org so this is the free tile server that open free map provides that essentially gives us this data about the map and so the JavaScript library gets these Vector
files back and then actually renders them onto the page now in this specific example we're using Vector tiles so if we look at the page itself it's just a canvas it's actually rendering these vectors directly onto a canvas here and then as I pan in Zoom it's actually updating what's displayed on this canvas here using that Vector information now vectors are just one way to display maps another way to get this data about the map is via tiles or images and so one of the services you might have used or heard of is called open street map and they actually have a tile server that provides raster tiles or essentially images and so you'll notice as I zoom in or pan around the map it's not making requests for these Vector files it's literally making requests for PNG images and then the JavaScript library is just adding these images to the page and so as I pan in Zoom you can see the actual loading is like squares that get popped in as I as I pan around because it's actually putting those images into the correct Place based on where I'm panning and zooming now the example on openstreetmap.org is using this Library called leaflet and you can see that it's actually just adding those images to the page here so it's not rendering to the
canvas literally as I pan and zoom around all of these images are in the Dom and then the library is just changing which images I can see as I pan and zoom around now from here that process just gets repeated right so as a user pans and zooms around the JavaScript library is going to figure out which parts of the map they need make the request out to that tile server get the tiles or the vectors back and then render them onto the page now the first spot in this whole flow where things will actually start to cost money is step one and so there are Services out there that will essentially charge you per request every single time their JavaScript library is loaded onto your page so every single time the user visits your your site or they refresh the page that counts towards your usage and they typically will have a limit now Google Maps used to be the best way to add free maps to your website but back in 2018 they actually changed their usage model and if you look at their pricing it costs to actually load the JavaScript SDK onto your web page so for 1,000 times that this library is requested it's going to cost you $7 a month and so you might imagine if you have a a larger website with more users the cost is going to go up more and more because every time this Library gets
client js - per load fees
loaded that's counting towards your Usage Now another service that's really popular is Mbox and they have usage fees as well now mapbox's usage fees are actually much more generous than Google so if you look at mapbox they allow you to do 50,000 monthly loads of their Library so you can load the library in with an API key and you can do that up to 50,000 times so even though they do have a fee the free tier works for a lot of Indie apps or smaller applications that don't necessarily need to load the library as many times but it's important to note that when you're using mapbox every time that JavaScript library is loaded that counts towards your monthly loads even when you're in development so if you're using that API key in your local development every time you hot reload the page that's counting towards your monthly request limit now another service that's out there is map Tyler cloud and they offer 5,000 sessions per month um I'm not completely sure on their terms and usage but I do think that means maybe 5,000 unique users not necessarily 5,000 loads of the library you'd have to look at their details to know more but as you can see all of these hosted map platforms charge you just to load the JavaScript library
itself and so you can instantly see that's why part of this is free and how what we we've built because we're just using map Libre which loads from wherever we host our source code right we install it from mpm it gets bundled in with our react app and we're not reaching out to any server to actually load the map Libre Library onto our web page because it's just getting served from where the rest of our JavaScript is getting served now the next spot here that can potentially cost money is the map tile server and then also the requests and the responses to that map tile server so first of all you need a server to host all of these images and Vector data and it costs money to host servers so that's one thing the next thing is every single request for tile data or vector data potentially will eat into your bandwidth cost that you're paying for wherever you're hosting your server and so any one of these services like Google Maps or mapbox has specific usage fees for the tile servers as well and so if you look at Google and then their map tile usage you can get 1,000 2D tile requests for 60 cents a month but again you essentially combine this with okay how many times is the map going to get loaded on my website per month and then each user how many tile requests are they going to make as you
tile server - usage fees
can see when a user is is navigating around it's making a lot of tile requests right now obviously some of these requests for tiles are going to be cashed especially if they've seen them before but each new request for something that hasn't been cashed is potentially incurring a fee so you can see for most M map applications you are going to be making a lot of tile server requests and so you only get 1,000 requests for free per month from Google Maps and so you can figure out how much of the map or my user is using how many requests does that make it can get expensive pretty quick now they do offer $200 of usage every month at no charge but again it depends on the number of users you have what exactly they're doing inside of your map and you might actually hit that limit pretty quickly now Mbox does have a much more generous free tier so if you look here at the vector tiles API you get 200,000 requests every month for free and so again this is probably enough for most Indie projects but if you have a more in demand application like the example I showed at the beginning of the video you might actually have a pretty big bill every month and so the question becomes how can we do this for free well first it comes down to the JavaScript library and I already showed you map Libre so map Libre is a way of loading maps onto
free map libraries
your site it's completely for free it's open source you just install it into your app and you're good to go now another popular option is leafletjs now leaflet typically works best with raster ties so that is tile servers that are serving up images not vectors though there are some plugins that'll allow you to do Vector tiles inside of leaflet as well and then another pretty popular option is open layers and so they also support tiles and vectors I haven't worked with them directly but they are another option now one of the reasons I found map libbre is because it's actually an open source Fork of the mapbox library and so I've built a lot of applications with mapbox but they do have that usage fee just for even adding their JavaScript library to your page and so back in 2020 they actually changed their license and they are no longer open source and map Libre was born out of that they essentially forked the last open- Source version of mapbox and have been maintaining it ever since and it's important to note that map Libra isn't just some JavaScript library maintained by like one person it's actually a working group and so there are people from map Tyler elastic stadia
maplibre short history - mapbox fork
Maps Microsoft Sarah's Imaging wear group jog Stam and design all of these people that have a vested interest in having good free maps on their websites and they're all part of the map Libre governing board and so this is what I like about map lib essentially if you're not doing anything inside of mapbox that require some of the newer features that they might have added in the Clos Source version you can actually just swap it out directly for map Libre and start getting free map loads on your website and then lastly how do we cover this expensive piece of a map tile server well there are actually Services out there that do provide free map tiles now if you look in the open street map Wiki they have a full list of raster tile providers and one of them is open street map Standard Tile layer so they actually do provide this tile server here it's free to use but they do have some usage requirements and usage limits if you've ever used leaflet JS they typically use this tile server in their opening example because if you're just getting started with maps it's a good way to go but if you look at their usage policy there are some things you need to take into account and so they typically recommend that you don't cash their tiles they do not allow bucal
free map tile servers
downloading and ultimately their tile server is for openstreetmap.org so I haven't really talked about what this project is but essentially this is kind of the Wikipedia of maps and so if a road ever gets removed or a new road gets added or there's new businesses or whatever else might change in the world of map data people can go here on open street map and actually add and make suggestions to the map and so all of that rich data about the world and and all of the the roads and countries and cities and towns and everything that you see when you're looking at a map open street map is like a completely open database of all of that information and so that free tile server is also serving this up and so they talk about in their usage requirements that you don't want to overload that tile server because then everybody that's using the openstreetmap.org website to actually add edits and make contributions to this open map uh potentially could run into issues and so you do have to look out for that now if you take a look at this page that has all these other types of tile servers it talks about their limitations whether or not they're free but in my experience I haven't been able to find a decent one that works well for
what is openstreetmap?
openfreemap is good
all of my needs and is completely free to use until now with open free map and so open free map provides a vector tile service with no usage limits and can essentially just be added to any application now there's a big wide world of vector tiles and Vector tile providers and so mattebox and their tiling service are a commercial Vector tile provider and so if you look in this article here on open street map you can see other types of providers that essentially will provide Vector tiles whether or not they have a usage fees or there are some free ones but maybe they're for non-commercial use now there is a vector tile specification that actually came out of mapbox and so Mbox has a standard for the vector tile format and they even have a GitHub repo here that describes that specification and so essentially the tiles that are served up by open free map are meeting that specification and can be used anywhere you might be using mapbox Vector tiles inside of your application now one of the first places I came across these Vector tile servers is map Tyler and so they actually have a a free tier but one of the cool things about them is they offer up all of these different kinds of styles of map and you can typically use these map Tyler Styles
vector tile services
vector tile specification
map styles
inside of your mapbox application and you can do a very similar thing with map Libra and so if you sign up for an account you can actually get an API key and of course they have usage limits for their tile servers but but they have all of these different styles that you can choose from and you can actually add these styles to your mapbox map or your map Libre map and so beyond that there is also the map Libre style specification which is essentially a way of describing how a map should look given some vectors and so with this style specification you can take all the parts of the vector map like the terrain and buildings and Roads and words and you can actually specify how they should be styled and this is also one of the things that open free map offers so if you look at open free map they actually have three different styles available they have Patron bright Liberty and then also 3D and if you look in their quick start guide they give you the URL to these specific style so if you want to add the Liberty style to your map you just pull in this URL and then if you want to choose some of these other styles you just change the style name on the end there but if we visit this URL we actually get back Json data and we
can see inside of here that it actually points to that open free map tile server and then it has all of the various layers of types of things and describes their various Styles and so this is one of the other cool things about combining open free map tiles with this style specification is you can actually create your own Styles based on this and host those completely for free as well so there's this editor called the mutnik editor and essentially you can take in one of these style specifications and then start changing all of the properties to create a custom style and map and so this is the base style here and so for instance if I zoom in on the map here and just click something like a street label or a city label I can then dial into the specs and change all of its properties I can change the color I can change the style let's actually just bump everything up and so now you can see all of the street labels are just like slightly larger and you can do this with everything right if I click on a road you can see specifically how that road is colored and then you can change the color of the road um so you have complete control over all of the Styles here and what's nice is as long as all the Styles you define still use open free map as the tile layer you can stylize it to your heart's content and
so that's actually what I've done in the examples that I'm going to show today is I have a dark style that I slightly modified from map Tyler that points to the open free map tiles but that style information is just hosted right inside of my repo here so if I refresh the page you can see that there's the a request to dark. Json this literally just lives inside of my repo and I have gone in and modified all the styles to change the colors and everything else but it still points to the open free map tiles there and so this is nice as well because you can just host the style information the same place that you're hosting the rest of your your front-end application so that's it for all the explanation let's see some actual code now if you look at open free map.org they have a quick start guide and they show you the vanilla JS version right so they're pulling in the map Libre library and then they're setting the styles to be that style file that points to the open free map tiles now if you look in the repo that's Linked In the description I have lots of different examples across different Frameworks and we're going to take a look at this vanilla example first now the one major difference for this example is I'm actually using JavaScript modules instead of just links in my HTML file so you can see in the
code examples
vanilla example
HTML file I'm still bringing in the CSS from a CDN and then I have my own custom Styles which are going to make that map full screen but if you take a look at app.js you can see here that it's specified as a module so es modules are supported every everywh this code just runs directly in the web browser without any sort of build process and then if we take a look at that app.js you can see that I'm actually just importing in that map Libre Library up at the top of the file now the example is almost exactly the same as the one you see on the open free map website we're just pulling that style directly in from open free maps but in this case after the page loads I'm getting the user's location just using this free API so essentially it just turns a user's IP address into a location it's just approximate it doesn't know your exact address but that's the nature of being on the Internet is given your IP address we approximate where you are so it uses that API it's approximately where the user is and then flies to that location and then shows a popup right there so you can see that I'm using the map libg namespace to create the map and then fly to that specific location and then also to create the pop-up now to run this vanilla example you do need some local
HTTP server you could use a python HTTP server you could use live server inside of vs code so and so if you have live server installed you can right click and say open with live server and then you'll see after the page loads it pops that map to show approximately where I am let's see how close I mean I'm in Denver I'll say that so it says yes I'm approximately there and so it's just this basic example but one of the main things I want to show is the fact that you can add your own Styles and so right here inside of this Styles directory I have dark. Json and inside of here I'm pointing to the open free map tiles but then I've gone in and updated all the layers to have this dark style and so if we pull that style file in instead of Liberty we can see the map updates and now we have some dark Styles you could even combine this with like a dark mode and light mode toggle right right so when you're in dark mode load the dark map Styles otherwise load the light map Styles uh but yeah I just wanted to show that as long as that Json file is accessible to the JavaScript code here it can load in those custom Styles now the next example I want to show you is leaflet and so if you're already using leaflet maybe you don't want to jump to
vanilla leaflet example
something like map lebre this example will show you how you can still use that tile layer inside of a leaflet app and so this example is set up in the exact same way except we do need two CSS file so we have to bring in the leaflet CSS and then we also bring in the map Libre CSS and so essentially the map libbre Library provides a layer that can be added to a leaflet map that will then render out the vector tiles and so I first load in leaflet I then load in the map lib Base Library and then I add in the compatibility layer the leaflet map lib GL and essentially what this Library does is it adds a map Libre GL method to your Global leaflet instance and then you can specifically add the layer there so you can see that I Enit a leaflet map with no tile layer then after that I add the map Libre GL layer which allows us to use the open free map tiles but you'll notice I'm also passing in attribution and so this just adds a link out to open free map but because open free map is based on top of open street map data you do need a link out to open street map at a minimum and if you look at the open free map website they do
show you the attribution that you need here and they mention you don't have to even mention open free map but it is nice to and I would because they're giving you free tiles come on and so if you want to run this example it's similar to the other vanilla example so I'll just use live server to open this up and we can see that it loads up and so now I have a a leaf FP map but it's using the open map tile server via that map Libre compatibility layer and you can see I have my attribution down here so people can click out to see where all this data and tiles are coming from now these next three examples use a front-end framework like react spelt or View and to do that typically you want to use a wrapper library and so whenever you're using a map library in your webpage typically that map library is interacting directly with the Dom or the canvas API right it's creating elements it's modifying the canvas and so if you want to use this library in conjunction with the front end framework there are some things you need to think about because when you're using a frontend framework that front end framework has some runtime and in the case of view and react there's also a virtual Dom runtime and so typically they don't want you to directly modify the Dom and if you're going to you then need to use some sort
maplibre framework adapters
of ref so that way it can stay in sync with your components and the virtual Dom model and so your application code is typically talking directly to that front end framework like view or react or spelt and then in turn it's modifying the Dom and creating elements and so if we want to use this front end framework in combination with something like appap Libre we need some sort of compatibility layer we need something that's going to be able to create refs and do the various things that you need to do to make sure that this frontend framework works in the right way and that looks a little bit like this so these libraries typically provide declarative component wrappers and so you're going to get like a capital M map component that you can just add into your page which in turn is going to create a map under the hood using the map Libre Library uh but it's going to use refs in all the right ways so you can actually integrate it into your react application so the application code that you're writing still just pulls in these declarative components like map and popup and marker but under the hood they are talking to the underlying library in this case map Libre and then also using refs and everything else that's required to make sure that it stays in sync with the framework itself and so now let's take a
react example
look at the react example and for this example I reached for vigl react map libbre and so this is a component library that gives us declarative components that wraps all the underlying map Libre functionality and if you look in the map Libre docs there actually are a few different libraries you can use but one of the the reasons I chose this one is because I'm very familiar with their react map GL library and so this is what a lot of people use if you're trying to combine react with mapbox and the react map Libre library is from the same creators but it's specifically for map Libre instead of mapbox and so that's why I chose this library but you can see after you install map libg and then also this Library you then get declarative components right they give you a map component that you can just add to your page and you can actually bind the styles to a state variable and then use reacts state system to change the center of the map or the zoom or other things like that and so if you take a look at our example I'm pulling the map in I have an initial view state so I just have this middle of the USA constant and so if for whatever reason I can't get the user's location it's going
to show the map kind of zoomed out where the the US is is in the middle there and from there you can see I'm passing in the map Styles just like in the vanilla example in the public directory is where those Styles live and so again you can customize these Styles just host them right there where your code is hosted and you'll be able to get some custom styles for your map now from there I have this custom component called you are here and this works in the same way as the vanilla example where it calls that API to get your approximate location and then we'll zoom in and add a popup at that location but you can see I'm using it in a declarative way and so if I go to this specific component first of all you can see that their Library provides a hook called use map and so essentially this is a context and because this component lives inside of the map component it can get access to the current map via the context and once I have access to the map I can do a lot of things like calling one of its built-in methods like fly two and then from there you can see that I have an internal State variable that's going to be the actual location of the popup so it defaults to the middle of the USA in the event that I couldn't get the user's location and then after I did get their
location I update that state and then we rerender it and so you can see here I'm using this declarative component called popup it comes from react map Libre and then they have a bunch of props that you can pass in so I'm just passing in the latitude and longitude and then the text that should be displayed but this is typically how you'll Implement things when you're working inside of react is you'll mainly be looking at the docs for react map Libre and and using all of the things that they have available for you like these Hooks and these declarative components now let's take a look at the view example and for the view example I am using this indoor equal view map Libre GL so this is the only one that's Linked In the map Libra documentation I found one other but because this one is linked here and if you're in the N ecosystem there's actually a n map lib Library it actually wraps this Library so that's why I chose to implement with this specific library but it's very similar to react map libate and that they provide these declarative components so you'll see that if I want a map we get access to this mg- map component that I can just add directly inside of my view components and so in my view example here I'm pulling in that mgl map rendering it defaulting to the
vue example
middle of the USA in the event that I I can't find the user's location in this case I'm also using some types directly from map Libra AGL and so I was running into an issue where because by default the inferred type is an array of numbers I couldn't pass it directly into the center location prop right here and so if I just use a type assertion then I'm able to actually pass that prop down here you can see we pass in the map style in a very similar way and then I have my custom component Ur are here and so that lives as a child of mgl map and that gives us access to the underlying map Libre map that that gets created inside of this component and we can access that inside of here so if we check out you are here the view map Libre GL Library exposes a hook called use map and so this gives us access to the map in the current context and so because our component is a child of that mgl map component we're going to get access to that map ref and then from there I just have a watch effect and so I'm basically looking for is the map loaded do we have access to that underlying ref and if we don't I'm just returning but the moment that we do I'm going to grab the user's location and if it's not the middle of the USA meaning I was able to get their location we are
going to update our state value here to be that location and then also call the fly to method on the underlying map libr map instance uh and then from there I'm using their declarative component called mgl popup and in this case I don't want this to display unless the map is actually loaded and I do need to pass in the coordinates now this is one of the things about these wrapper libraries that you might come come across is they decide what props to expose for any given component and they also get to decide what are the default props because under the hood this needs to create a popup using map Libre but they're giving us this declarative way of doing it and so the default Behavior here is that it will not close on click and so I do have to pass in false and then I can also pass in the coordinates which will display that popup on the page now again just like the react example I have a public folder where I'm storing those Styles so if you want to load in the custom Styles you can just do it directly from the same code base now let's take a look at the spelt example so for this example example I decided to use the rapper Library called spelt map Libre and if you look in the map libbre docs there are a couple different libraries linked but this one I found to have most of the components
svelte example
that you would need and I did come across one issue with the pop-up component I'll show you how I fix but for the most part this seems to have everything that you need and so we can take a look here at my entry point I'm pulling in map Libre from their library and so this is a component that's going to render a map right there pass in the middle of the USA location by default and then pass in the style URL as well and because this is a v app just like the react app and the view app we can put those Styles inside of the public folder and then I have my own custom component called you are here so let's take a look at that now spelt map Libre provides this function called map context and essentially this is a spelt store that will give you access to the current map that you are rendering inside of and so this is what will allow us to actually fly to the user's location once we have it now you'll notice here that I'm not using the declarative pop-up component here I just had some issues getting it to actually render the text it was showing up as blank and so if you take a look at their examples they do do have some examples that have popups like this is an example of a pop-up here you can see pretty much anytime they have a popup it's rendered inside of a marker and so I don't know
if internally they're kind of just assuming that anytime you have a popup it needs to be a child of a marker which will probably be the case in most scenarios but for my example I didn't even want a marker I just wanted a popup and so I couldn't get it to render but the nice thing is because all of these libraries are going to give you access to that underlying map Libre instance you can still do all the same map libbre things if for whatever reason they're components aren't working or maybe they don't provide a component that does what you need it to do so you can see here that I'm pulling popup directly from the map Libre library and then I'm just creating an instance to it and they have a method that allows you to add it to the map and so this is kind of combining using the spelt map Libre library with just direct vanilla map Libre code to kind of add things dynamically and the last thing I want to point you to is awesome map Libre so this is a GitHub repo that has a bunch of various libraries and tools and things that you might use in conjunction with map Libre and so if there's something specific you're trying to do or a specific uh framework or Library you're trying to integrate with you'll probably find a resource there on this page now that's
awesome-maplibre
thanks
it for this video hopefully I've given you enough to get up and going with open free map and so you can go out and build your own completely free map applications uh definitely drop a star over on the open free map GitHub repo uh hypernut is providing the service completely free and that's really awesome so you can also sponsor them so if you are using open free map in your applications and you're getting some good use out of it definitely go sponsor hypernut on GitHub as well all right that's all I got for this one I'll see you in the next one [Music]
Quote it with credit and a link. Quoting Syntax