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 var map, layers; // TODO: remove globals
14 $(document).ready(function () {
16 var params = OSM.mapParams();
23 map.attributionControl.setPrefix('');
26 layer: new L.OSM.Mapnik({
31 name: I18n.t("javascripts.map.base.standard")
33 layer: new L.OSM.CycleMap({
34 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
38 name: I18n.t("javascripts.map.base.cycle_map")
40 layer: new L.OSM.TransportMap({
41 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
44 keyid: "transportmap",
45 name: I18n.t("javascripts.map.base.transport_map")
47 layer: new L.OSM.MapQuestOpen({
48 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'>",
52 name: I18n.t("javascripts.map.base.mapquest")
55 layers[0].layer.addTo(map);
57 $("#map").on("resized", function () {
61 L.control.customZoom({position: 'topright'})
64 var uiPane = $('#map-ui')[0];
85 L.control.scale().addTo(map);
87 map.on("moveend layeradd layerremove", updateLocation);
89 if (!params.object_zoom) {
91 var bbox = L.latLngBounds([params.minlat, params.minlon],
92 [params.maxlat, params.maxlon]);
104 map.setView([params.lat, params.lon], params.zoom);
109 var foundLayer = false;
110 for (var i = 0; i < layers.length; i++) {
111 if (params.layers.indexOf(layers[i].layer.options.code) >= 0) {
112 map.addLayer(layers[i].layer);
115 map.removeLayer(layers[i].layer);
119 map.addLayer(layers[0].layer);
124 marker = L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map);
128 addObjectToMap(params.object, map, { zoom: params.object_zoom });
133 $("body").on("click", "a.set_position", function (e) {
136 var data = $(this).data();
137 var centre = L.latLng(data.lat, data.lon);
139 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
140 map.fitBounds([[data.minLat, data.minLon],
141 [data.maxLat, data.maxLon]]);
143 map.setView(centre, data.zoom);
146 if (data.type && data.id) {
147 addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
151 map.removeLayer(marker);
154 marker = L.marker(centre, {icon: getUserIcon()}).addTo(map);
157 function updateLocation() {
158 var center = map.getCenter().wrap();
159 var zoom = map.getZoom();
160 var layers = getMapLayers();
161 var extents = map.getBounds().wrap();
163 updatelinks(center, zoom, layers, extents, params.object);
165 var expiry = new Date();
166 expiry.setYear(expiry.getFullYear() + 10);
167 $.cookie("_osm_location", [center.lng, center.lat, zoom, layers].join("|"), {expires: expiry});
170 function remoteEditHandler() {
171 var extent = map.getBounds();
174 $("#linkloader").load(function () { loaded = true; });
175 $("#linkloader").attr("src", "http://127.0.0.1:8111/load_and_zoom?" +
176 querystring.stringify({
177 left: extent.getWest(),
178 bottom: extent.getSouth(),
179 right: extent.getEast(),
180 top: extent.getNorth()
183 setTimeout(function () {
184 if (!loaded) alert(I18n.t('site.index.remote_failed'));
190 $("a[data-editor=remote]").click(remoteEditHandler);
192 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
196 $(window).resize(handleResize);
198 $("#search_form").submit(function () {
199 var bounds = map.getBounds();
201 $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
202 $("#sidebar_content").load($(this).attr("action"), {
203 query: $("#query").val(),
204 minlon: bounds.getWest(),
205 minlat: bounds.getSouth(),
206 maxlon: bounds.getEast(),
207 maxlat: bounds.getNorth()
213 if ($("#query").val()) {
214 $("#search_form").submit();
217 // Focus the search field for browsers that don't support
218 // the HTML5 'autofocus' attribute
219 if (!("autofocus" in document.createElement("input"))) {
224 function getMapBaseLayer() {
225 for (var i = 0; i < layers.length; i++) {
226 if (map.hasLayer(layers[i].layer)) {
232 function getMapLayers() {
233 var layerConfig = "";
234 for (var i in map._layers) { // TODO: map.eachLayer
235 var layer = map._layers[i];
236 if (layer.options && layer.options.code) {
237 layerConfig += layer.options.code;