2 //= require leaflet.layers
3 //= require leaflet.share
4 //= require leaflet.note
5 //= require leaflet.locate
6 //= require index/browse
7 //= require index/export
9 //= require index/notes
11 var map, layers; // TODO: remove globals
13 $(document).ready(function () {
15 var params = OSM.mapParams();
22 map.attributionControl.setPrefix('');
25 layer: new L.OSM.Mapnik({
30 name: I18n.t("javascripts.map.base.standard")
32 layer: new L.OSM.CycleMap( {
33 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
37 name: I18n.t("javascripts.map.base.cycle_map")
39 layer: new L.OSM.TransportMap({
40 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
42 keyid: "transportmap",
44 name: I18n.t("javascripts.map.base.transport_map")
46 layer: new L.OSM.MapQuestOpen({
47 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'>",
51 name: I18n.t("javascripts.map.base.mapquest")
54 layers[0].layer.addTo(map);
56 $("#map").on("resized", function () {
60 L.control.zoom({position: 'topright'})
63 L.OSM.layers({position: 'topright', layers: layers})
67 getUrl: function(map) {
68 return 'http://osm.org/' + querystring.stringify({
69 lon: map.getCenter().lng,
70 lat: map.getCenter().lat
83 L.control.scale().addTo(map);
85 map.on("moveend layeradd layerremove", updateLocation);
87 if (!params.object_zoom) {
89 var bbox = L.latLngBounds([params.minlat, params.minlon],
90 [params.maxlat, params.maxlon]);
102 map.setView([params.lat, params.lon], params.zoom);
107 setMapLayers(params.layers);
111 marker = L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map);
115 addObjectToMap(params.object, map, { zoom: params.object_zoom });
120 $("body").on("click", "a.set_position", function (e) {
123 var data = $(this).data();
124 var centre = L.latLng(data.lat, data.lon);
126 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
127 map.fitBounds([[data.minLat, data.minLon],
128 [data.maxLat, data.maxLon]]);
130 map.setView(centre, data.zoom);
133 if (data.type && data.id) {
134 addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
138 map.removeLayer(marker);
141 marker = L.marker(centre, {icon: getUserIcon()}).addTo(map);
144 function updateLocation() {
145 var center = map.getCenter().wrap();
146 var zoom = map.getZoom();
147 var layers = getMapLayers();
148 var extents = map.getBounds().wrap();
150 updatelinks(center, zoom, layers, extents, params.object);
152 var expiry = new Date();
153 expiry.setYear(expiry.getFullYear() + 10);
154 $.cookie("_osm_location", [center.lng, center.lat, zoom, layers].join("|"), {expires: expiry});
157 function remoteEditHandler() {
158 var extent = map.getBounds();
161 $("#linkloader").load(function () { loaded = true; });
162 $("#linkloader").attr("src", "http://127.0.0.1:8111/load_and_zoom?" +
163 querystring.stringify({
164 left: extent.getWest(),
165 bottom: extent.getSouth(),
166 right: extent.getEast(),
167 top: extent.getNorth()
170 setTimeout(function () {
171 if (!loaded) alert(I18n.t('site.index.remote_failed'));
177 $("a[data-editor=remote]").click(remoteEditHandler);
179 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
183 $(window).resize(handleResize);
185 $("#search_form").submit(function () {
186 var bounds = map.getBounds();
188 $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
189 $("#sidebar_content").load($(this).attr("action"), {
190 query: $("#query").val(),
191 minlon: bounds.getWest(),
192 minlat: bounds.getSouth(),
193 maxlon: bounds.getEast(),
194 maxlat: bounds.getNorth()
200 if ($("#query").val()) {
201 $("#search_form").submit();
204 // Focus the search field for browsers that don't support
205 // the HTML5 'autofocus' attribute
206 if (!("autofocus" in document.createElement("input"))) {
211 function getMapBaseLayer() {
212 for (var i = 0; i < layers.length; i++) {
213 if (map.hasLayer(layers[i].layer)) {
219 function getMapLayers() {
220 var layerConfig = "";
221 for (var i = 0; i < layers.length; i++) {
222 if (map.hasLayer(layers[i].layer)) {
223 layerConfig += layers[i].layerCode;
229 function setMapLayers(layerConfig) {
230 var foundLayer = false;
231 for (var i = 0; i < layers.length; i++) {
232 if (layerConfig.indexOf(layers[i].layerCode) >= 0) {
233 map.addLayer(layers[i].layer);
236 map.removeLayer(layers[i].layer);
240 map.addLayer(layers[0].layer);