[GIS] GeoJSON in Leaflet TileLayer

geojsonleaflet

I'm just starting to work with leaflet and have a basic question about showing a geoJSON file. First, I found this excellent article with example code:

http://bl.ocks.org/lxbarth/4019660

However I have a more basic/beginner question, in that example the set the JSON file through this url:

http://polymaps.appspot.com/county/{z}/{x}/{y}.json

However, the file I converted (from shapefile to geojson using quantum gis) is just a .geojson extension. I'm clearly missing something here, perhaps a way to convert the geojson file into a tile format? Can someone elaborate as to how I'm supposed to do this or what my missing step is? Do I need to be hosting the geoJSON file on a server in order to do this?

If my file is called polygons.geojson, how would I go about using it in the above reference code? Thanks!

Best Answer

I guess your question is a bit confused because you mixed 2 notions which are not necessarily associated. To be clear :

  • on one part, you can read and display geojson files with leaflet (geojson is a format). Following this URL, you will find basics examples : http://leafletjs.com/examples/geojson.html
  • on the other part, leaflet is also able to understand and display tiles through the TileLayer object. Tiles are a way to optimize the map rendering (independently of the format).

On the given example, the map displays geojson polymaps tiles, mixing both geojson format and tiles mechanism.

According to your needs you can choose to :

  1. display the full geojson file (easy to implement, ok for small size files),
  2. create your own tiles with TileStache. Regarding geojson tiles creation a question was asked before, you may refer to the accepted answer and given links.