]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/embed.js.erb
Add report a problem link to attribution, code nicked from osm.ch
[rails.git] / app / assets / javascripts / embed.js.erb
1 //= require leaflet
2 //= require leaflet.osm
3
4 window.onload = function () {
5   var query = (window.location.search || '?').substr(1),
6       args  = {};
7
8   var pairs = query.split('&');
9   for (var i = 0; i < pairs.length; i++) {
10     var parts = pairs[i].split('=');
11     args[parts[0]] = decodeURIComponent(parts[1] || '');
12   }
13
14   var map = L.map("map");
15   map.attributionControl.setPrefix('');
16   map.removeControl(map.attributionControl);
17
18   if (!args.layer || args.layer === "mapnik" || args.layer === "osmarender") {
19     new L.OSM.Mapnik().addTo(map);
20   } else if (args.layer === "cyclemap" || args.layer === "cycle map") {
21     new L.OSM.CycleMap().addTo(map);
22   } else if (args.layer === "transportmap") {
23     new L.OSM.TransportMap().addTo(map);
24   } else if (args.layer === "mapquest") {
25     new L.OSM.MapQuestOpen().addTo(map);
26   } else if (args.layer === "hot") {
27     new L.OSM.HOT().addTo(map);
28   }
29
30   if (args.marker) {
31     L.marker(args.marker.split(','), {icon: L.icon({
32       iconUrl: <%= asset_path('images/marker-icon.png').to_json %>,
33       iconSize: new L.Point(25, 41),
34       iconAnchor: new L.Point(12, 41),
35       shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,
36       shadowSize: new L.Point(41, 41)
37     })}).addTo(map);
38   }
39
40   if (args.bbox) {
41     var bbox = args.bbox.split(',');
42     map.fitBounds([L.latLng(bbox[1], bbox[0]),
43                    L.latLng(bbox[3], bbox[2])]);
44   } else {
45     map.fitWorld();
46   }
47   (new L.Control.OSMReportAProblem({})).addTo(map);
48 };
49
50 L.Control.OSMReportAProblem = L.Control.Attribution.extend({
51         options: {
52                 position: 'bottomright',
53                 prefix: '<a href="http://www.openstreetmap.org/fixthemap?lat={x}&lon={y}&zoom={z}">Report a problem</a>'
54         },
55         _layerAdd: function(e)
56         {
57                 if (e.layer.getAttribution) {
58                         this.addAttribution(e.layer.getAttribution());
59                 }
60         },
61         _layerRemove: function(e)
62         {
63                 if (e.layer.getAttribution) {
64                         this.removeAttribution(e.layer.getAttribution());
65                 }
66         },
67         onAdd: function (map) {
68                 this._container = L.DomUtil.create('div', 'leaflet-control-attribution');
69                 L.DomEvent.disableClickPropagation(this._container);
70
71                 // TODO ugly, refactor
72                 for (var i in map._layers) {
73                         if (map._layers[i].getAttribution) {
74                                 this.addAttribution(map._layers[i].getAttribution());
75                         }
76                 }
77
78                 this._update();
79                 map.on('moveend', this._update, this);
80                 map.on('layeradd', this._layerAdd, this);
81                 map.on('layerremove', this._layerRemove, this);
82
83                 return this._container;
84         },
85         _update: function () {
86                 if (!this._map) { return; }
87
88                 var attribs = [];
89
90                 for (var i in this._attributions) {
91                         if (this._attributions[i]) {
92                                 attribs.push(i);
93                         }
94                 }
95
96                 var prefixAndAttribs = [];
97
98                 if (this.options.prefix) {
99                         prefixAndAttribs.push(this.options.prefix);
100                 }
101                 if (attribs.length) {
102                         prefixAndAttribs.push(attribs.join(', '));
103                 }
104
105                 this._container.innerHTML = prefixAndAttribs.join(' | ').replace('{x}', this._map.getCenter().lat).replace('{y}', this._map.getCenter().lng).replace('{z}', this._map.getZoom());
106         }
107
108     });