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