]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/map.js.erb
Bundle leaflet css in a single asset
[rails.git] / app / assets / javascripts / map.js.erb
1 // Leaflet extensions
2 L.LatLngBounds.include({
3   getSouthLat: function () {
4     return this._southWest.lat;
5   },
6
7   getWestLng: function () {
8     return this._southWest.lng;
9   },
10
11   getNorthLat: function () {
12     return this._northEast.lat;
13   },
14
15   getEastLng: function () {
16     return this._northEast.lng;
17   },
18
19   toBBOX: function () {
20     var decimal = 6;
21     var mult = Math.pow(10, decimal);
22     var xmin = Math.round(this.getWestLng() * mult) / mult;
23     var ymin = Math.round(this.getSouthLat() * mult) / mult;
24     var xmax = Math.round(this.getEastLng() * mult) / mult;
25     var ymax = Math.round(this.getNorthLat() * mult) / mult;
26     return xmin + "," + ymin + "," + xmax + "," + ymax;
27   },
28
29   getSize: function () {
30     return (this._northEast.lat - this._southWest.lat) *
31            (this._northEast.lng - this._southWest.lng);
32   }
33 });
34
35 L.Bounds.include({
36   getWidth: function () {
37    return this.max.x - this.min.x;
38   },
39
40   getHeight: function () {
41    return this.max.y - this.min.y;
42   }
43 });
44
45 var map;
46
47 var layers = [
48   {
49     klass: L.OSM.Mapnik,
50     attribution: "",
51     keyid: "mapnik",
52     layerCode: "M",
53     name: I18n.t("javascripts.map.base.standard")
54   },
55   {
56     klass: L.OSM.CycleMap,
57     attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
58     keyid: "cyclemap",
59     layerCode: "C",
60     name: I18n.t("javascripts.map.base.cycle_map")
61   },
62   {
63     klass: L.OSM.TransportMap,
64     attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
65     keyid: "transportmap",
66     layerCode: "T",
67     name: I18n.t("javascripts.map.base.transport_map")
68   },
69   {
70     klass: L.OSM.MapQuestOpen,
71     attribution: "Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='http://developer.mapquest.com/content/osm/mq_logo.png'>",
72     keyid: "mapquest",
73     layerCode: "Q",
74     name: I18n.t("javascripts.map.base.mapquest")
75   }
76 ];
77
78 function createMap(divName, options) {
79   options = $.extend({zoomControl: false, panZoomControl: true, layerControl: true}, options);
80
81   map = L.map(divName, $.extend({}, options, {panControl: false, zoomsliderControl: false, maxZoom: 18}));
82
83   if (map.attributionControl) {
84     map.attributionControl.setPrefix(''); // For tmcw
85   }
86
87   if (options.panZoomControl) {
88     new L.Control.Pan().addTo(map);
89     new L.Control.Zoomslider({stepHeight: 7}).addTo(map);
90   }
91
92   var layersControl = L.control.layers();
93
94   if (options.layerControl) {
95     layersControl.addTo(map);
96   }
97
98   for (var i = 0; i < layers.length; i++) {
99     layers[i].layer = new (layers[i].klass)(layers[i]);
100     if (i == 0) {
101       layers[i].layer.addTo(map);
102     }
103     layersControl.addBaseLayer(layers[i].layer, layers[i].name);
104   }
105
106   $("#" + divName).on("resized", function () {
107     map.invalidateSize();
108   });
109
110   return map;
111 }
112
113 function getArrowIcon() {
114   return L.icon({
115     iconUrl: <%= asset_path('arrow.png').to_json %>,
116     iconSize: [25, 22],
117     iconAnchor: [22, 20]
118   });
119 }
120
121 function addMarkerToMap(position, icon, description) {
122   var marker = L.marker(position, icon ? {icon: icon} : null).addTo(map);
123
124   if (description) {
125     marker.bindPopup(description);
126   }
127
128   return marker;
129 }
130
131 function removeMarkerFromMap(marker) {
132   map.removeLayer(marker);
133 }
134
135 function addObjectToMap(object, zoom, callback) {
136   $.ajax({
137     url: OSM.apiUrl(object),
138     dataType: "xml",
139     success: function (xml) {
140       var layer = new L.OSM.DataLayer(xml, {
141         style: {
142           strokeColor: "blue",
143           strokeWidth: 3,
144           strokeOpacity: 0.5,
145           fillOpacity: 0.2,
146           fillColor: "lightblue",
147           pointRadius: 6
148         }
149       });
150
151       var bounds = layer.getBounds();
152
153       if (zoom) {
154         map.fitBounds(bounds);
155       }
156
157       if (callback) {
158         callback(bounds);
159       }
160
161       layer.addTo(map);
162     }
163   });
164 }
165
166 function addBoxToMap(bounds) {
167   var box = L.rectangle(bounds, {
168     weight: 2,
169     color: '#e90',
170     fillOpacity: 0
171   });
172
173   box.addTo(map);
174
175   return box;
176 }
177
178 function getMapBaseLayer() {
179   for (var i = 0; i < layers.length; i++) {
180     if (map.hasLayer(layers[i].layer)) {
181       return layers[i];
182     }
183   }
184 }
185
186 function getMapLayers() {
187   for (var i = 0; i < layers.length; i++) {
188     if (map.hasLayer(layers[i].layer)) {
189       return layers[i].layerCode;
190     }
191   }
192
193   return "";
194 }
195
196 function setMapLayers(layerConfig) {
197   for (var i = 0; i < layers.length; i++) {
198     if (~layerConfig.indexOf(layers[i].layerCode)) {
199       map.addLayer(layers[i].layer);
200     } else {
201       map.removeLayer(layers[i].layer);
202     }
203   }
204 }