2 //= require leaflet.customzoom
3 //= require leaflet.locate
4 //= require leaflet.layers
5 //= require leaflet.key
6 //= require leaflet.note
7 //= require leaflet.share
8 //= require index/browse
9 //= require index/export
11 //= require index/notes
13 $(document).ready(function () {
14 var params = OSM.mapParams();
16 var map = L.map("map", {
21 map.attributionControl.setPrefix('');
28 name: I18n.t("javascripts.map.base.standard")
31 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
34 name: I18n.t("javascripts.map.base.cycle_map")
36 new L.OSM.TransportMap({
37 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
39 keyid: "transportmap",
40 name: I18n.t("javascripts.map.base.transport_map")
42 new L.OSM.MapQuestOpen({
43 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'>",
46 name: I18n.t("javascripts.map.base.mapquest")
52 map.noteLayer = new L.LayerGroup({code: 'N'});
53 map.dataLayer = new L.OSM.DataLayer(null);
55 $("#map").on("resized", function () {
59 L.control.customZoom({position: 'topright'})
62 L.control.locate({position: 'topright'})
65 var uiPane = $('#map-ui')[0];
91 map.on('moveend layeradd layerremove', updateLocation);
93 map.markerLayer = L.layerGroup().addTo(map);
95 if (!params.object_zoom) {
97 var bbox = L.latLngBounds([params.minlat, params.minlon],
98 [params.maxlat, params.maxlon]);
110 map.setView([params.lat, params.lon], params.zoom);
115 var foundLayer = false;
116 for (var i = 0; i < layers.length; i++) {
117 if (params.layers.indexOf(layers[i].options.code) >= 0) {
118 map.addLayer(layers[i]);
121 map.removeLayer(layers[i]);
125 map.addLayer(layers[0]);
130 L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map.markerLayer);
134 addObjectToMap(params.object, map, { zoom: params.object_zoom });
139 $("body").on("click", "a.set_position", setPositionLink(map));
141 $("a[data-editor=remote]").click(function(e) {
142 remoteEditHandler(map.getBounds());
146 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
147 remoteEditHandler(map.getBounds());
150 $(window).resize(handleResize);
152 $("#search_form").submit(submitSearch(map));
155 if ($("#query").val()) {
156 $("#search_form").submit();
159 // Focus the search field for browsers that don't support
160 // the HTML5 'autofocus' attribute
161 if (!("autofocus" in document.createElement("input"))) {
165 initializeBrowse(map);
166 initializeNotes(map);
169 function getMapBaseLayerId(map) {
170 for (var i in map._layers) { // TODO: map.eachLayer
171 var layer = map._layers[i];
172 if (layer.options && layer.options.keyid) {
173 return layer.options.keyid;
178 function getMapLayers(map) {
179 var layerConfig = '';
180 for (var i in map._layers) { // TODO: map.eachLayer
181 var layer = map._layers[i];
182 if (layer.options && layer.options.code) {
183 layerConfig += layer.options.code;
189 // generate a cookie-safe string of map state
190 function cookieContent(map) {
191 var center = map.getCenter().wrap();
192 return [center.lng, center.lat, map.getZoom(), getMapLayers(map)].join('|');
195 function updateLocation() {
196 updatelinks(this.getCenter().wrap(),
199 this.getBounds().wrap(), {});
201 var expiry = new Date();
202 expiry.setYear(expiry.getFullYear() + 10);
203 $.cookie("_osm_location", cookieContent(this), { expires: expiry });
206 function setPositionLink(map) {
208 var data = $(this).data(),
209 center = L.latLng(data.lat, data.lon);
211 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
212 map.fitBounds([[data.minLat, data.minLon],
213 [data.maxLat, data.maxLon]]);
215 map.setView(center, data.zoom);
218 if (data.type && data.id) {
219 addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
222 map.markerLayer.clearLayers();
223 L.marker(center, {icon: getUserIcon()}).addTo(map.markerLayer);
225 return e.preventDefault();
229 function submitSearch(map) {
231 var bounds = map.getBounds();
233 $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
234 $("#sidebar_content").load($(this).attr("action"), {
235 query: $("#query").val(),
236 minlon: bounds.getWestLng(),
237 minlat: bounds.getSouthLat(),
238 maxlon: bounds.getEastLng(),
239 maxlat: bounds.getNorthLat()
242 return e.preventDefault();