Search code examples
apiopenstreetmaptile

Alternative to hardcoding url to openstreetmap


OSM's usage policy states (https://operations.osmfoundation.org/policies/tiles/):

"Recommended: Do not hardcode any URL to tile.openstreetmap.org as doing so will limit your ability to react if the service is disrupted or blocked. In particular, switching should be possible without requiring a software update."

I am wondering 1) what is referred to in this passage and 2) how to avoid it.

I am guessing something like the following would be considered "hardcoding a URL to tile.openstreetmap.org".

var map = L.map('mapid', {
      center: [39.3, -76.60],
      zoom: 11,
      minZoom: 3,
      maxZoom: 15,
      layers: [
        L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
          attribution:
            '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
        })
      ]
    });

If so, what's the proper alternative in this case?


Solution

  • This mainly means that the tiles provided by the OSMF may cease to be served (in general or for you) at any time, be it because of technical reasons or policy reasons or capacity reasons.

    Now as a software developer you should be able to therefore switch the tile provider at any time.

    Several examples:

    • you run (and code) a website yourself. Your code example is fine as you will be able to change it anytime (just be aware about browser caching)
    • you develop a website for a third party. If you don't constantly work for them, they should get the option to switch the tile provider with coding, e.g. if you use wordpress, wrap it in a plugin where you have a TileLayerURL as a editable setting in the backend for a wordpress admin. Same if you build a plugin/theme etc. for third parties to be used.
    • and now it gets really important: never use a hardcoded TileLayerURL like the one above in an app development or software development as you would need to update the app/software to change the TileLayerURL (and every client would need to run that update).

    By the way, the TileLayerURL should be

    https://tile.openstreetmap.org/{z}/{x}/{y}.png
    

    as the OSMF is trying to get away from the (a|b|c) subdomains. And here you already see the problem: if you would have used your old TileLayerURL and the OSMF would start to stop serving requests against (a|b|c).tile.osm.org, you wouldn't get a map anymore.