--- layout: tutorial_v2 title: Extending Leaflet, New Layers --- This tutorial assumes you've read the [theory of Leaflet class inheritance](./extending-1-classes.html). In Leaflet, a "layer" is anything that moves around when the map is moved around. Before seeing how to create them from scratch, it's easier to explain how to do simple extensions. ## "Extension methods" A few of the Leaflet classes have so-called "extension methods": entry points for writing code for sub-classes. One of them is `L.TileLayer.getTileUrl()`. This method is called internally by `L.TileLayer` whenever a new tile needs to know which image to load. By making a subclass of `L.TileLayer` and rewriting its `getTileUrl()` function, we can create custom behaviour. Let's illustrate with a custom `L.TileLayer` that will display random kitten images from [PlaceKitten](): L.TileLayer.Kitten = L.TileLayer.extend({ getTileUrl: function(coords) { var i = Math.ceil( Math.random() * 4 ); return "http://placekitten.com/256/256?image=" + i; }, getAttribution: function() { return "PlaceKitten" } }); L.tileLayer.kitten = function() { return new L.TileLayer.Kitten(); } L.tileLayer.kitten().addTo(map); {% include frame.html url="kittenlayer.html" %} Normally, `getTileLayer()` receives the tile coordinates (as `coords.x`, `coords.y` and `coords.z`) and generates a tile URL from them. In our example, we ignore those and simply use a random number to get a different kitten every time. ### Splitting away the plugin code In the previous example, `L.TileLayer.Kitten` is defined in the same place as it's used. For plugins, it's better to split the plugin code into its own file, and include that file when it's used. For the KittenLayer, you should create a file like `L.KittenLayer.js` with: L.TileLayer.Kitten = L.TileLayer.extend({ getTileUrl: function(coords) { var i = Math.ceil( Math.random() * 4 ); return "http://placekitten.com/256/256?image=" + i; }, getAttribution: function() { return "PlaceKitten" } }); And then, include that file when showing a map: … … ### `L.GridLayer` and DOM elements Another extension method is `L.GridLayer.createTile()`. Where `L.TileLayer` assumes that there is a grid of images (as `` elements), `L.GridLayer` doesn't assume that - it allows creating grids of any kind of [HTML Elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element). `L.GridLayer` allows creating grids of ``s, but grids of [`