Wednesday, 6 May 2015

openstreetmap - Load external tileJSON file in Mapbox


I have tiles generated by Maperitive from an OSM file, and there's a nice tiles.json generated with them which specifies neatly the tiles-path, bounding box, minzoom, maxzoom, center, etc. The format is identical to the tileJSON specification given here: https://www.mapbox.com/guides/an-open-platform/#tilejson


But it just blows that the example stopped a step short and inline'd the JSON code into the example's source code, hence rendering it (pun intended) useless. No clue on https://github.com/mapbox/tilejson-spec either. All web searches for this lead to these sites; the rest digress into GeoJSON which is something else entirely. Why in the world would anyone go through the trouble of using JSON if one wasn't importing it from an external file? (no, please don't try to answer that.. it's not the main question. Focus!)


So, a humble request: Please re-write this example code given on mapbox.com, to show an external JSON file being loaded. (And please put in a word with someone there if you know them to post a proper example on the website)



















Edit: Included jquery.min.js and changed code as advised; am getting a "not well-formed" error in console. I reckon jquery doesn't like tileJSON. This is why full examples ought to be given :P. Please see and advise what needs to change here. Is there a more generic JSON-loader we can use instead of jquery? I believe we're very close to getting this.


var tilejson;
$.getJSON('pune/tiles.json', function(data) {
tilejson = data;
});
console.log(tilejson.name);


L.mapbox.map('map', tilejson, {
scrollWheelZoom: false
});

Here is the content of tiles.json (loading local tiles, generated by Maperitive; tested to be valid JSON as per http://codebeautify.org/jsonviewer):


{
"tilejson": "1.0.0",
"name": "Pune 1 to 16",
"description": "Made with Maperitive",

"attribution": "Map data © OpenStreetMap contributors",
"tiles": [
"pune/{z}/{x}/{y}.png"
],
"minzoom": 1,
"maxzoom": 16,
"bounds": [
73.59366818720622,
18.33281593564951,
74.07630689997964,

18.77938751279949
],
"center": [
73.83498754359293,
18.5561017242245,
1
]
}


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...