]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/map.js.erb
Merge branch 'leaflet'
[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 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   }
99
100   for (var i = 0; i < layers.length; i++) {
101     layers[i].layer = new (layers[i].klass)(layers[i]);
102     layersControl.addBaseLayer(layers[i].layer, layers[i].name);
103   }
104
105   layers[0].layer.addTo(map);
106
107   $("#" + divName).on("resized", function () {
108     map.invalidateSize();
109   });
110
111   return map;
112 }
113
114 function getUserIcon(url) {
115   return L.icon({
116     iconUrl: url || <%= asset_path('marker-red.png').to_json %>,
117     iconSize: [25, 41],
118     iconAnchor: [12, 41],
119     popupAnchor: [1, -34],
120     shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,
121     shadowSize: [41, 41]
122   });
123 }
124
125 function addObjectToMap(object, zoom, callback) {
126   $.ajax({
127     url: OSM.apiUrl(object),
128     dataType: "xml",
129     success: function (xml) {
130       var layer = new L.OSM.DataLayer(xml, {
131         style: {
132           strokeColor: "blue",
133           strokeWidth: 3,
134           strokeOpacity: 0.5,
135           fillOpacity: 0.2,
136           fillColor: "lightblue",
137           pointRadius: 6
138         }
139       });
140
141       var bounds = layer.getBounds();
142
143       if (zoom) {
144         map.fitBounds(bounds);
145       }
146
147       if (callback) {
148         callback(bounds);
149       }
150
151       layer.addTo(map);
152     }
153   });
154 }
155
156 function addBoxToMap(bounds) {
157   var box = L.rectangle(bounds, {
158     weight: 2,
159     color: '#e90',
160     fillOpacity: 0
161   });
162
163   box.addTo(map);
164
165   return box;
166 }
167
168 function getMapBaseLayer() {
169   for (var i = 0; i < layers.length; i++) {
170     if (map.hasLayer(layers[i].layer)) {
171       return layers[i];
172     }
173   }
174 }
175
176 function getMapLayers() {
177   for (var i = 0; i < layers.length; i++) {
178     if (map.hasLayer(layers[i].layer)) {
179       return layers[i].layerCode;
180     }
181   }
182
183   return "";
184 }
185
186 function setMapLayers(layerConfig) {
187   for (var i = 0; i < layers.length; i++) {
188     if (layerConfig.indexOf(layers[i].layerCode) >= 0) {
189       map.addLayer(layers[i].layer);
190     } else {
191       map.removeLayer(layers[i].layer);
192     }
193   }
194 }