]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/map.js.erb
Update to Leaflet 0.5
[rails.git] / app / assets / javascripts / map.js.erb
1 // Leaflet extensions
2 L.extend(L.LatLngBounds.prototype, {
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.extend(L.Bounds.prototype, {
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 L.Icon.Default.imagePath = <%= "#{asset_prefix}/images".to_json %>;
46
47 var map;
48
49 var layers = [
50   {
51     klass: L.OSM.Mapnik,
52     attribution: "",
53     keyid: "mapnik",
54     layerCode: "M",
55     name: I18n.t("javascripts.map.base.standard")
56   },
57   {
58     klass: L.OSM.CycleMap,
59     attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
60     keyid: "cyclemap",
61     layerCode: "C",
62     name: I18n.t("javascripts.map.base.cycle_map")
63   },
64   {
65     klass: L.OSM.TransportMap,
66     attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
67     keyid: "transportmap",
68     layerCode: "T",
69     name: I18n.t("javascripts.map.base.transport_map")
70   },
71   {
72     klass: L.OSM.MapQuestOpen,
73     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'>",
74     keyid: "mapquest",
75     layerCode: "Q",
76     name: I18n.t("javascripts.map.base.mapquest")
77   }
78 ];
79
80 function createMap(divName, options) {
81   options = $.extend({zoomControl: true, panZoomControl: true, layerControl: true}, options);
82
83   map = L.map(divName, $.extend({}, options, {panControl: false, zoomsliderControl: false, maxZoom: 18}));
84
85   if (map.attributionControl) {
86     map.attributionControl.setPrefix('');
87   }
88
89   if (options.panZoomControl) {
90     new L.Control.Pan().addTo(map);
91     new L.Control.Zoomslider({stepHeight: 7}).addTo(map);
92   }
93
94   var layersControl = L.control.layers();
95
96   if (options.layerControl) {
97     layersControl.addTo(map);
98     map.layersControl = layersControl;
99   }
100
101   for (var i = 0; i < layers.length; i++) {
102     layers[i].layer = new (layers[i].klass)(layers[i]);
103     layersControl.addBaseLayer(layers[i].layer, layers[i].name);
104   }
105
106   layers[0].layer.addTo(map);
107
108   $("#" + divName).on("resized", function () {
109     map.invalidateSize();
110   });
111
112   return map;
113 }
114
115 function getUserIcon(url) {
116   return L.icon({
117     iconUrl: url || <%= asset_path('marker-red.png').to_json %>,
118     iconSize: [25, 41],
119     iconAnchor: [12, 41],
120     popupAnchor: [1, -34],
121     shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,
122     shadowSize: [41, 41]
123   });
124 }
125
126 function addObjectToMap(object, zoom, callback) {
127   $.ajax({
128     url: OSM.apiUrl(object),
129     dataType: "xml",
130     success: function (xml) {
131       var layer = new L.OSM.DataLayer(xml, {
132         style: {
133           strokeColor: "blue",
134           strokeWidth: 3,
135           strokeOpacity: 0.5,
136           fillOpacity: 0.2,
137           fillColor: "lightblue",
138           pointRadius: 6
139         }
140       });
141
142       var bounds = layer.getBounds();
143
144       if (zoom) {
145         map.fitBounds(bounds);
146       }
147
148       if (callback) {
149         callback(bounds);
150       }
151
152       layer.addTo(map);
153     }
154   });
155 }
156
157 function addBoxToMap(bounds) {
158   var box = L.rectangle(bounds, {
159     weight: 2,
160     color: '#e90',
161     fillOpacity: 0
162   });
163
164   box.addTo(map);
165
166   return box;
167 }
168
169 function getMapBaseLayer() {
170   for (var i = 0; i < layers.length; i++) {
171     if (map.hasLayer(layers[i].layer)) {
172       return layers[i];
173     }
174   }
175 }
176
177 function getMapLayers() {
178   for (var i = 0; i < layers.length; i++) {
179     if (map.hasLayer(layers[i].layer)) {
180       return layers[i].layerCode;
181     }
182   }
183
184   return "";
185 }
186
187 function setMapLayers(layerConfig) {
188   var foundLayer = false;
189   for (var i = 0; i < layers.length; i++) {
190     if (layerConfig.indexOf(layers[i].layerCode) >= 0) {
191       map.addLayer(layers[i].layer);
192       foundLayer = true;
193     } else {
194       map.removeLayer(layers[i].layer);
195     }
196   }
197   if (!foundLayer) {
198     map.addLayer(layers[0].layer);
199   }
200 }