]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/map.js.erb
96959b4e1beaab73573a645f2629148b5aa45c31
[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     class: L.OSM.Mapnik,
50     attribution: "",
51     keyid: "mapnik",
52     layerCode: "M",
53     name: I18n.t("javascripts.map.base.standard")
54   },
55   {
56     class: 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     class: 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     class: 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   map = L.map(divName, options);
80
81   if (map.attributionControl) {
82     map.attributionControl.setPrefix(''); // For tmcw
83   }
84
85   var layersControl = L.control.layers();
86
87   if (!options || options.layerControl !== false) {
88     layersControl.addTo(map);
89   }
90
91   for (var i = 0; i < layers.length; i++) {
92     layers[i].layer = new (layers[i].class)(layers[i]);
93     if (i == 0) {
94       layers[i].layer.addTo(map);
95     }
96     layersControl.addBaseLayer(layers[i].layer, layers[i].name);
97   }
98
99   $("#" + divName).on("resized", function () {
100     map.invalidateSize();
101   });
102
103   return map;
104 }
105
106 function getArrowIcon() {
107   return L.icon({
108     iconUrl: <%= asset_path('arrow.png').to_json %>,
109     iconSize: [25, 22],
110     iconAnchor: [22, 20]
111   });
112 }
113
114 function addMarkerToMap(position, icon, description) {
115   var marker = L.marker(position, icon ? {icon: icon} : null).addTo(map);
116
117   if (description) {
118     marker.bindPopup(description);
119   }
120
121   return marker;
122 }
123
124 function removeMarkerFromMap(marker) {
125   map.removeLayer(marker);
126 }
127
128 function addObjectToMap(object, zoom, callback) {
129   $.ajax({
130     url: OSM.apiUrl(object),
131     dataType: "xml",
132     success: function (xml) {
133       var layer = new L.OSM.DataLayer(xml, {
134         style: {
135           strokeColor: "blue",
136           strokeWidth: 3,
137           strokeOpacity: 0.5,
138           fillOpacity: 0.2,
139           fillColor: "lightblue",
140           pointRadius: 6
141         }
142       });
143
144       var bounds = layer.getBounds();
145
146       if (zoom) {
147         map.fitBounds(bounds);
148       }
149
150       if (callback) {
151         callback(bounds);
152       }
153
154       layer.addTo(map);
155     }
156   });
157 }
158
159 function addBoxToMap(bounds) {
160   var box = L.rectangle(bounds, {
161     weight: 2,
162     color: '#e90',
163     fillOpacity: 0
164   });
165
166   box.addTo(map);
167
168   return box;
169 }
170
171 function getMapBaseLayer() {
172   for (var i = 0; i < layers.length; i++) {
173     if (map.hasLayer(layers[i].layer)) {
174       return layers[i];
175     }
176   }
177 }
178
179 function getMapLayers() {
180   for (var i = 0; i < layers.length; i++) {
181     if (map.hasLayer(layers[i].layer)) {
182       return layers[i].layerCode;
183     }
184   }
185
186   return "";
187 }
188
189 function setMapLayers(layerConfig) {
190   for (var i = 0; i < layers.length; i++) {
191     if (~layerConfig.indexOf(layers[i].layerCode)) {
192       map.addLayer(layers[i].layer);
193     } else {
194       map.removeLayer(layers[i].layer);
195     }
196   }
197 }