Embed JavaScript Map for Free Using MapLibre
Introduction
In addition to Google Map API, MapLibre GL JS is an open-source library for publishing maps on our websites. The maps are usually in the vector format and fast rendering in browser has become possible thanks to GPU.
In my previous blog post “Embed Google Map for Free”, I have discussed how to embed a static Google Map on our website for free. However, embedding a dynamic map using JavaScript requires using Google Map API and Google Map API is not free.
In this blog post, I would like to discuss how to use MapLibre to create custom dynamic maps on our websites using JavaScript mostly for free.
MapLibre GL JS
Quick Start
Add a marker to a Map could be just as simple as the follows. Because the vectorized map is hosted on MapTiler, we would have to register an account, create an API key, and use the API key in the code. Fortunately, there is free plan available and there are a decent number of free requests given to the user.
1 |
|
The API keys can be abused by other users, because the API key is inside the HTML code. It is highly recommended using one key per web application and set allowed HTTP origins to prevent stealing of the key.
Finally, to make the embedded map compatible with the existing webpage, we used iframe
for integration.
1 | <div style="overflow-x:auto;"> |
The embedded map for this example looks like this.
Custom Example
Similar to the map of places that I have visited recently in my previous blog post “Embed Google Map for Free”, I created another one using MapLibre GL JS.
Miscellaneous
The free street map style from MapTiler lacks details comparing to Google Map. However, MapTiler allows the user to custom different map styles for different advanced purposes, which is something that Google Map could not do.
References
Embed JavaScript Map for Free Using MapLibre
https://leimao.github.io/blog/Embed-JavaScript-Map-Free-Using-MapLibre/