2 //= require leaflet.layers
3 //= require leaflet.share
4 //= require leaflet.note
5 //= require leaflet.locate
6 //= require leaflet.customzoom
7 //= require index/browse
8 //= require index/export
10 //= require index/notes
12 $(document).ready(function () {
13 var params = OSM.mapParams();
15 var map = L.map("map", {
20 map.attributionControl.setPrefix('');
27 name: I18n.t("javascripts.map.base.standard")
30 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
33 name: I18n.t("javascripts.map.base.cycle_map")
35 new L.OSM.TransportMap({
36 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
38 keyid: "transportmap",
39 name: I18n.t("javascripts.map.base.transport_map")
41 new L.OSM.MapQuestOpen({
42 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'>",
45 name: I18n.t("javascripts.map.base.mapquest")
51 $("#map").on("resized", function () {
55 L.control.customZoom({position: 'topright'})
58 var uiPane = $('#map-ui')[0];
71 L.control.note({ position: 'topright' }).addTo(map);
72 L.control.locate({ position: 'topright' }).addTo(map);
73 L.control.scale().addTo(map);
75 map.on('moveend layeradd layerremove', updateLocation);
77 map.markerLayer = L.layerGroup().addTo(map);
79 if (!params.object_zoom) {
81 var bbox = L.latLngBounds([params.minlat, params.minlon],
82 [params.maxlat, params.maxlon]);
94 map.setView([params.lat, params.lon], params.zoom);
99 var foundLayer = false;
100 for (var i = 0; i < layers.length; i++) {
101 if (params.layers.indexOf(layers[i].options.code) >= 0) {
102 map.addLayer(layers[i]);
105 map.removeLayer(layers[i]);
109 map.addLayer(layers[0]);
114 L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map.markerLayer);
118 addObjectToMap(params.object, map, { zoom: params.object_zoom });
123 $("body").on("click", "a.set_position", setPositionLink(map));
125 $("a[data-editor=remote]").click(function(e) {
126 remoteEditHandler(map.getBounds());
130 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
131 remoteEditHandler(map.getBounds());
134 $(window).resize(handleResize);
136 $("#search_form").submit(submitSearch(map));
139 if ($("#query").val()) {
140 $("#search_form").submit();
143 // Focus the search field for browsers that don't support
144 // the HTML5 'autofocus' attribute
145 if (!("autofocus" in document.createElement("input"))) {
150 function getMapBaseLayerId(map) {
151 for (var i in map._layers) { // TODO: map.eachLayer
152 var layer = map._layers[i];
153 if (layer.options && layer.options.keyid) {
154 return layer.options.keyid;
159 function getMapLayers(map) {
160 var layerConfig = '';
161 for (var i in map._layers) { // TODO: map.eachLayer
162 var layer = map._layers[i];
163 if (layer.options && layer.options.code) {
164 layerConfig += layer.options.code;
170 // generate a cookie-safe string of map state
171 function cookieContent(map) {
172 var center = map.getCenter().wrap();
173 return [center.lng, center.lat, map.getZoom(), getMapLayers(map)].join('|');
176 function updateLocation() {
177 updatelinks(this.getCenter().wrap(),
180 this.getBounds().wrap(), {});
182 var expiry = new Date();
183 expiry.setYear(expiry.getFullYear() + 10);
184 $.cookie("_osm_location", cookieContent(this), { expires: expiry });
187 function setPositionLink(map) {
189 var data = $(this).data(),
190 center = L.latLng(data.lat, data.lon);
192 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
193 map.fitBounds([[data.minLat, data.minLon],
194 [data.maxLat, data.maxLon]]);
196 map.setView(center, data.zoom);
199 if (data.type && data.id) {
200 addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
203 map.markerLayer.clearLayers();
204 L.marker(center, {icon: getUserIcon()}).addTo(map.markerLayer);
206 return e.preventDefault();
210 function submitSearch(map) {
212 var bounds = map.getBounds();
214 $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
215 $("#sidebar_content").load($(this).attr("action"), {
216 query: $("#query").val(),
217 minlon: bounds.getWestLng(),
218 minlat: bounds.getSouthLat(),
219 maxlon: bounds.getEastLng(),
220 maxlat: bounds.getNorthLat()
223 return e.preventDefault();