Leaflet Tunisia Map: Add TMaps Tiles in Minutes
Build a Leaflet map of Tunisia with TMaps raster tiles. Follow the setup, configure your API key and zoom limits, and troubleshoot common tile errors.

Add a Map of Tunisia to Leaflet with TMaps Tiles
A delivery app, property listing site, or store locator usually needs more than a generic world map. In Tunisia, users need to find local streets, neighborhoods, and road names - so the map has to feel reliable at the street level. Leaflet gives you the interactive map controls. TMaps provides the raster tiles for Tunisia. You can connect the two with a small amount of JavaScript, but the setup details matter: the wrong domain in your key (or a zoom level where there are no tiles) can leave you staring at a grey, empty map.
This guide shows a working setup for Leaflet + TMaps, explains how tile coordinates are used, and walks through the most common reasons tiles don't load. It also covers the practical limits of raster tiles - especially on high-density screens and when you need more styling control - so you can decide whether this approach fits your product before you build around it.
What do you need to add TMaps tiles to Leaflet?
You need Leaflet, a TMaps API key, and a page served over HTTPS once you deploy. Leaflet handles the interactive part - panning, zoom controls, touch gestures, and requesting the right tiles for what the user is viewing. TMaps provides the tile images. When something breaks, it's easier to diagnose if you check those two parts separately.
How raster tiles work
A raster tile is a small, pre-rendered image - commonly 256 × 256 pixels - that covers one square of the map. At each zoom level, the map is split into a grid of these tiles.
Leaflet figures out which tiles it needs for the current viewport and uses the standard XYZ URL pattern:
https://tiles.tmaps.tn/r-tile/{z}/{x}/{y}?api_key=YOUR_API_KEY
Here, z is the zoom level, and x and y identify the tile's column and row in the grid for that zoom. You don't calculate them yourself - Leaflet fills them into the URL template as the user pans and zooms.
A practical consequence: tiles are loaded as separate network requests, not as one big image for the entire country. So you can end up with a partially rendered map when only some tiles fail. When that happens, check the browser's Network panel and the tile layer configuration instead of assuming the whole tile service is down.
Create and restrict an API key
Create an account in the TMaps console, then generate an API key. Add every domain that will display the map to the key's authorized domain list (for example, your production site, a separate app subdomain, and your local development host).
Don't assume wildcards will work for subdomains. The setup described by TMaps doesn't accept wildcard domains, so add each hostname you actually use.
Also use HTTPS. If the request is sent over plain HTTP, the provider can return a 403.
One more thing: a browser-side map key is visible to anyone who inspects the page or watches the network requests. That's normal for a client-side map integration. Domain restrictions help prevent easy reuse on unrelated sites, but they don't make the key secret. Treat key configuration as part of your deployment setup, and review the provider's controls and terms before relying on anything beyond that.
How do you build a Leaflet map of Tunisia?
Once Leaflet is available on the page and you have a key, the basic setup is short. The example below centers the map on Tunis, sets an initial zoom level of 12, and adds the TMaps raster layer. Make sure Leaflet's CSS and JavaScript are loaded before this script runs.
<div id="map"></div>
<style>
html, body, #map {
height: 100%;
margin: 0;
}
</style>
<script>
const API_KEY = 'YOUR_API_KEY';
const map = L.map('map').setView([36.8002, 10.1815], 12);
L.tileLayer(
`https://tiles.tmaps.tn/r-tile/{z}/{x}/{y}?api_key=${API_KEY}`,
{
maxZoom: 18,
attribution: '© TMaps'
}
).addTo(map);
</script>
Replace the placeholder with your key and confirm that the hostname serving the page is authorized. The map container needs a height. If it has no height, the map may initialize but stay invisible because the browser has no space to render into. height: 100% works only when the parent elements also have a usable height; if your layout is more complex, give #map a fixed or responsive height instead.
Choose a useful starting view
The example uses central Tunis and zoom level 12 as a starting point. Your product might need a different view. A store locator could start near the user's selected city. A property search might start centered around the area represented by results.
Decide the initial center and zoom based on the task your map supports - not just because something looks good in a demo. Also test the default view at the actual viewport sizes you support. At the same zoom level, a mobile screen covers less geographic area than a desktop screen, so users may need a different default to orient themselves. If your app updates the map after a search or location selection, keep the map state and the rest of the UI in sync.
Keep the tile layer configuration explicit
Set maxZoom to 18 because the raster tiles in this setup are available up to that level. If you allow zoom beyond what the tiles cover, users can end up requesting empty tiles or a grey grid. Matching the map's zoom range to the tile coverage avoids a confusing experience.
Attribution is also part of the layer setup. Keep the provider credit visible as required by the provider's terms. In a production UI, make sure the attribution stays readable and isn't hidden behind custom controls or mobile overlays.
How do you troubleshoot blank or incomplete tiles?
A grey map usually means the map container rendered, but tile requests didn't return usable images. The fastest way to narrow it down is to use the browser developer tools - especially the Network panel - and inspect one failed tile request: look at the status code and the URL. Don't change multiple settings at once; verify one thing at a time.
Check the key, domain, and protocol
Start with the access configuration:
- Confirm
YOUR_API_KEYhas been replaced with your generated key and that it hasn't been revoked. - Check the exact hostname in the browser is on the authorized domain list. Development, staging, production, and separate subdomains may need separate entries.
- Confirm both your page and tile requests use HTTPS.
- Check the response status for a failed tile request. In this setup, a 401 typically points to a missing or invalid key, while a 403 often indicates a domain authorization or protocol issue.
Be careful with hostnames. example.com, www.example.com, and app.example.com are different hostnames for authorization purposes if each is used to serve the map. A configuration that works on a developer machine can fail after deployment if the production hostname wasn't added.
Add a tile error signal
Tile failures can be easy to miss, especially when only some requests fail. Add an event listener to the layer so you get a clear console message during development:
const tiles = L.tileLayer(
`https://tiles.tmaps.tn/r-tile/{z}/{x}/{y}?api_key=${API_KEY}`,
{ maxZoom: 18, attribution: '© TMaps' }
);
tiles.once('tileerror', () => {
console.error(
'TMaps tiles could not be loaded: check the API key and authorized domains.'
);
});
tiles.addTo(map);
A one-time listener is helpful for a quick integration check, but it's not a full monitoring strategy. One failure could be a transient network problem, while a systematic authorization issue will affect many requests. During testing, compare the console message with the Network panel, and try multiple locations and zoom levels.
Distinguish access failures from zoom and layout problems
If you see request errors, focus on the key, domain, and HTTPS configuration. If requests succeed but the map still looks wrong, check the tile URL template, the map container's dimensions, and the zoom range.
For example:
- If the map looks fine until you zoom in, you may be requesting levels above 18.
- If you see a blank area from the start, it might be a zero-height container rather than a tile service problem.
Separating these issues saves time: first confirm that tile requests succeed, then confirm that Leaflet is placing the returned tiles into the correct container. One controlled check per layer (page layout, then Leaflet setup, then tile access) makes the problem easier to isolate.
What are the limits of raster tiles, and when should you switch?
Raster tiles work well when you want a conventional interactive map and the provider's rendered style fits your needs. The trade-off is that each tile is just an image. You can pan and zoom through Leaflet, but you can't directly style individual map features in the browser like you can with vector tiles.
High-density screens and custom styling
The TMaps raster tiles described here don't include Retina or @2x versions. On some high-density screens, the map may look less sharp than UI elements rendered at full device pixel density. If sharpness on target devices matters, test on those devices rather than judging solely from a standard desktop display.
Raster imagery also limits customization. If you need to change colors dynamically, rotate/tilt the map, or style features at runtime, vector tiles are usually a better fit. Switching to vectors is more work, though - don't move purely because vector tiles are newer. Switch when the product requirement truly justifies it.
Caching and mobile data use
Tile responses include long Cache-Control headers, which can help browsers reuse tiles on later visits. That can reduce repeat downloads, but the exact benefit depends on browser behavior, cache state, and how users move around the map. Don't assume every tile will stay available forever.
On mobile, test with realistic journeys. Panning across a large area can trigger lots of tile requests, even when each tile is small. Use the caching features your chosen platform provides where they're supported, and measure network behavior under the connection conditions your users actually have. Think of caching as a performance benefit, not a substitute for understanding the provider's usage terms or for planning offline behavior.
Reuse the XYZ layer in other map libraries
The tile URL uses the XYZ scheme, so it can often be reused in other mapping tools that support XYZ tile sources, such as OpenLayers or compatible MapLibre and mobile SDK setups. The library changes, but the basic relationship between zoom, tile column, and tile row stays the same.
That portability helps if your frontend architecture changes. It doesn't mean every mapping feature is automatically interchangeable, though. Check how the new library handles attribution, zoom limits, caching, and URL templates. And if you need a shareable map view, your app still has to decide how to represent and restore center/zoom in its own URL or state model.
Conclusion
Putting a Tunisia map into Leaflet is a straightforward integration, but reliable results depend on the basics: an authorized key, HTTPS, a map container with a real height, and a zoom limit that matches the available tiles. Start with the minimal layer, verify tile requests in the browser, and only add complexity when you know the integration is working.
If the provider's visual style works for your use case, raster tiles are usually a practical choice. If you need custom feature styling, rotation/tilt, or sharper rendering on high-density screens, evaluate vector tiles early - otherwise the map becomes a core dependency you can't easily unwind later.