File: tilecache.js

package info (click to toggle)
modestmaps-js 3.3.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,184 kB
  • ctags: 1,465
  • sloc: makefile: 58
file content (47 lines) | stat: -rw-r--r-- 1,546 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
MM.TileCacheMapProvider = function(template, subdomains) {

    // utility functions...
    function addZeros(i, zeros) {
        if (zeros === undefined) {
            zeros = 3;
        }
        var s = i.toString();
        while (s.length < zeros)
        {
            s = '0' + s;
        }
        return s;
    }

    function tilePad(i) {
        return addZeros(parseInt(i / 1000000, 10)) + '/' +
            addZeros(parseInt(i / 1000, 10)) + '/' + addZeros(i % 1000);
    }

    // this is like a call to 'super', kinda...
    // we end up initializing a basic modestmaps.js MapProvider with
    // a getTileURL function that knows about the above utility functions
    MM.MapProvider.call(this, function(coord) {
        coord = this.sourceCoordinate(coord);
        if (!coord) {
            return null;
        }

        var mod = coord.copy();

        // fill in coordinates into URL
        var url = template.replace('{Z}', mod.zoom)
                          .replace('{X}', tilePad(mod.column))
                          .replace('{Y}', tilePad(Math.pow(2, mod.zoom) - 1 - mod.row));
        // replace the {S} portion of the url with the appropriate subdomain
        if (url.indexOf('{S}') > -1) {
            var subdomain = (subdomains && subdomains.length > 0) ?
              subdomains[parseInt(mod.row + mod.column, 10) % subdomains.length] : '';
            url = url.replace('{S}', subdomain ? subdomain + '.' : '');
        }

        return url;
    });
};

MM.extend(MM.TileCacheMapProvider, MM.MapProvider);