]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/map.js.erb
Move popup anchor definition to icon
[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: false, 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(''); // For tmcw
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     if (i == 0) {
103       layers[i].layer.addTo(map);
104     }
105     layersControl.addBaseLayer(layers[i].layer, layers[i].name);
106   }
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: [19, 25],
119     iconAnchor: [11, 26],
120     popupAnchor: [-1, -20]
121   });
122 }
123
124 function addObjectToMap(object, zoom, callback) {
125   $.ajax({
126     url: OSM.apiUrl(object),
127     dataType: "xml",
128     success: function (xml) {
129       var layer = new L.OSM.DataLayer(xml, {
130         style: {
131           strokeColor: "blue",
132           strokeWidth: 3,
133           strokeOpacity: 0.5,
134           fillOpacity: 0.2,
135           fillColor: "lightblue",
136           pointRadius: 6
137         }
138       });
139
140       var bounds = layer.getBounds();
141
142       if (zoom) {
143         map.fitBounds(bounds);
144       }
145
146       if (callback) {
147         callback(bounds);
148       }
149
150       layer.addTo(map);
151     }
152   });
153 }
154
155 function addBoxToMap(bounds) {
156   var box = L.rectangle(bounds, {
157     weight: 2,
158     color: '#e90',
159     fillOpacity: 0
160   });
161
162   box.addTo(map);
163
164   return box;
165 }
166
167 function getMapBaseLayer() {
168   for (var i = 0; i < layers.length; i++) {
169     if (map.hasLayer(layers[i].layer)) {
170       return layers[i];
171     }
172   }
173 }
174
175 function getMapLayers() {
176   for (var i = 0; i < layers.length; i++) {
177     if (map.hasLayer(layers[i].layer)) {
178       return layers[i].layerCode;
179     }
180   }
181
182   return "";
183 }
184
185 function setMapLayers(layerConfig) {
186   for (var i = 0; i < layers.length; i++) {
187     if (~layerConfig.indexOf(layers[i].layerCode)) {
188       map.addLayer(layers[i].layer);
189     } else {
190       map.removeLayer(layers[i].layer);
191     }
192   }
193 }