aboutsummaryrefslogtreecommitdiffstats
path: root/web/js/map-bing-ol.js
blob: c44cf96f8fc4e416d1655ea988187260f3dbae6e (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
fixmystreet.maps.config = function() {
    fixmystreet.controls = [
        new OpenLayers.Control.Attribution(),
        new OpenLayers.Control.ArgParserFMS(),
        new OpenLayers.Control.Navigation(),
        new OpenLayers.Control.PermalinkFMS('map'),
        new OpenLayers.Control.PanZoomFMS({id: 'fms_pan_zoom' })
    ];
    /* Linking back to around from report page, keeping track of map moves */
    if ( fixmystreet.page == 'report' ) {
        fixmystreet.controls.push( new OpenLayers.Control.PermalinkFMS('key-tool-problems-nearby', '/around') );
    }
};

(function() {
    $(function(){
        $('#map_layer_toggle').click(function(e) {
            e.preventDefault();
            var $this = $(this);
            if ($this.text() == translation_strings.map_aerial) {
                $this.text(translation_strings.map_roads);
                fixmystreet.map.setBaseLayer(fixmystreet.map.layers[1]);
            } else {
                $this.text(translation_strings.map_aerial);
                fixmystreet.map.setBaseLayer(fixmystreet.map.layers[0]);
            }
        });
        // If page loaded with Aerial as starting, rather than default road
        if ($('#map_layer_toggle').text() == translation_strings.map_roads) {
            fixmystreet.map.setBaseLayer(fixmystreet.map.layers[1]);
        }
    });
})();

OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
    tile_base: '//t{S}.ssl.ak.dynamic.tiles.virtualearth.net/comp/ch/${id}?mkt=en-US&it=G,L&src=t&shading=hill&og=969&n=z',
    attributionTemplate: '${logo}${copyrights}',

    setMap: function() {
        OpenLayers.Layer.XYZ.prototype.setMap.apply(this, arguments);
        this.updateAttribution();
    },

    _updateAttribution: function(copyrights, logo) {
        this.attribution = OpenLayers.String.format(this.attributionTemplate, {
            logo: logo,
            copyrights: copyrights
        });
        if (this.map) {
            this.map.events.triggerEvent("changelayer", {
                layer: this,
                property: "attribution"
            });
        }
    },

    updateAttribution: function() {
        var year = (new Date()).getFullYear();
        var copyrights = '&copy; ' + year + ' <a href="https://www.bing.com/maps/">Microsoft</a>, HERE';
        var logo = '<a href="https://www.bing.com/maps/"><img border=0 src="//dev.virtualearth.net/Branding/logo_powered_by.png"></a>';
        this._updateAttribution(copyrights, logo);
    },

    initialize: function(name, options) {
        var url = [];
        options = OpenLayers.Util.extend({
            /* Below line added to OSM's file in order to allow minimum zoom level */
            maxResolution: 156543.03390625/Math.pow(2, options.zoomOffset || 0),
            numZoomLevels: 20,
            sphericalMercator: true,
            buffer: 0
        }, options);
        var newArguments = [name, url, options];
        OpenLayers.Layer.XYZ.prototype.initialize.apply(this, newArguments);
    },

    get_quadkey: function(x, y, level) {
        var key = '';
        for (var i = level; i > 0; i--) {
            var digit = 0;
            var mask = 1 << (i - 1);
            if ((x & mask) !== 0) {
                digit++;
            }
            if ((y & mask) !== 0) {
                digit += 2;
            }
            key += digit;
        }
        return key;
    },

    getURL: function (bounds) {
        var res = this.map.getResolution();
        var x = Math.round((bounds.left - this.maxExtent.left) /
            (res * this.tileSize.w));
        var y = Math.round((this.maxExtent.top - bounds.top) /
            (res * this.tileSize.h));
        var z = this.serverResolutions !== null ?
            OpenLayers.Util.indexOf(this.serverResolutions, res) :
            this.map.getZoom() + this.zoomOffset;

        var url = this.get_urls(bounds, z);
        var s = '' + x + y + z;
        url = this.selectUrl(s, url);
       
        var id = this.get_quadkey(x, y, z);
        var path = OpenLayers.String.format(url, {'id': id, 'x': x, 'y': y, 'z': z});
        return path;
    },

    get_urls: function(bounds, z) {
        var urls = [];
        for (var i=0; i<4; i++) {
            urls.push(this.tile_base.replace('{S}', i));
        }
        return urls;
    },

    CLASS_NAME: "OpenLayers.Layer.Bing"
});

OpenLayers.Layer.BingAerial = OpenLayers.Class(OpenLayers.Layer.Bing, {
    tile_base: '//t{S}.ssl.ak.dynamic.tiles.virtualearth.net/comp/ch/${id}?mkt=en-US&it=A,G,L&src=t&og=969&n=z',

    setMap: function() {
        OpenLayers.Layer.Bing.prototype.setMap.apply(this, arguments);
        this.map.events.register("moveend", this, this.updateAttribution);
    },

    updateAttribution: function() {
        var z = this.map.getZoom() + this.zoomOffset;
        var year = (new Date()).getFullYear();
        var copyrights = '&copy; ' + year + ' <a href="https://www.bing.com/maps/">Microsoft</a>, HERE, ';
        if (z >= 13) {
            copyrights += 'Maxar, CNES Distribution Airbus DS';
        } else {
            copyrights += 'Earthstar Geographics SIO';
        }
        var logo = '<a href="https://www.bing.com/maps/"><img border=0 src="//dev.virtualearth.net/Branding/logo_powered_by.png"></a>';
        this._updateAttribution(copyrights, logo);
    },

    CLASS_NAME: "OpenLayers.Layer.BingAerial"
});

fixmystreet.layer_options = [
  { map_type: OpenLayers.Layer.Bing },
  { map_type: OpenLayers.Layer.BingAerial }
];