1 L.OSM.share = function (options) {
2 var control = L.control(options),
3 marker = L.marker([0, 0], {draggable: true}),
4 locationFilter = new L.LocationFilter({
9 control.onAdd = function (map) {
10 var $container = $('<div>')
11 .attr('class', 'control-share');
14 .attr('class', 'control-button mobile-hide')
16 .attr('title', I18n.t('javascripts.share.title'))
17 .html('<span class="icon share"></span>')
19 .appendTo($container);
22 .attr('class', 'share-ui');
25 .attr('class', 'sidebar_heading')
29 .text(I18n.t('javascripts.close'))
30 .attr('class', 'icon close')
31 .bind('click', toggle))
34 .text(I18n.t('javascripts.share.title')));
38 var $linkSection = $('<div>')
39 .attr('class', 'section share-link')
43 .text(I18n.t('javascripts.share.link'))
44 .appendTo($linkSection);
46 var $form = $('<form>')
47 .attr('class', 'standard-form')
48 .appendTo($linkSection);
51 .attr('class', 'form-row')
55 .attr('for', 'link_marker')
58 .attr('id', 'link_marker')
59 .attr('type', 'checkbox')
60 .bind('change', toggleMarker))
61 .append(I18n.t('javascripts.share.include_marker')));
64 .attr('class', 'share-tabs')
67 .attr('class', 'active')
68 .attr('for', 'long_input')
69 .attr('id', 'long_link')
70 .text(I18n.t('javascripts.share.long_link')))
72 .attr('for', 'short_input')
73 .attr('id', 'short_link')
74 .text(I18n.t('javascripts.share.short_link')))
76 .attr('for', 'embed_html')
78 .text(I18n.t('javascripts.share.embed')))
79 .on('click', 'a', function(e) {
81 var id = '#' + $(this).attr('for');
82 $linkSection.find('.share-tabs a')
83 .removeClass('active');
84 $(this).addClass('active');
85 $linkSection.find('.share-tab')
87 $linkSection.find('.share-tab:has(' + id + ')')
89 .find('input, textarea')
94 .attr('class', 'form-row share-tab')
95 .css('display', 'block')
98 .attr('id', 'long_input')
100 .on('click', select));
103 .attr('class', 'form-row share-tab')
106 .attr('id', 'short_input')
107 .attr('type', 'text')
108 .on('click', select));
111 .attr('class', 'form-row share-tab')
115 .attr('id', 'embed_html')
116 .on('click', select))
119 .attr('class', 'deemphasize')
120 .text(I18n.t('javascripts.share.paste_html'))
121 .appendTo($linkSection));
125 var $imageSection = $('<div>')
126 .attr('class', 'section share-image')
130 .text(I18n.t('javascripts.share.image'))
131 .appendTo($imageSection);
134 .attr('class', 'standard-form')
135 .attr('action', '/export/finish')
136 .attr('method', 'post')
137 .appendTo($imageSection);
140 .attr('class', 'form-row')
144 .attr('for', 'image_filter')
147 .attr('id', 'image_filter')
148 .attr('type', 'checkbox')
149 .bind('change', toggleFilter))
150 .append(I18n.t('javascripts.share.custom_dimensions')));
153 .attr('class', 'form-row')
157 .attr('for', 'mapnik_format')
158 .text(I18n.t('javascripts.share.format')))
159 .append($('<select>')
160 .attr('name', 'mapnik_format')
161 .attr('id', 'mapnik_format')
162 .append($('<option>').val('png').text('PNG').prop('selected', true))
163 .append($('<option>').val('jpeg').text('JPEG'))
164 .append($('<option>').val('svg').text('SVG'))
165 .append($('<option>').val('pdf').text('PDF')));
168 .attr('class', 'form-row')
171 .attr('for', 'mapnik_scale')
172 .text(I18n.t('javascripts.share.scale')))
175 .attr('name', 'mapnik_scale')
176 .attr('id', 'mapnik_scale')
177 .attr('type', 'text')
178 .on('change', update));
180 ['minlon', 'minlat', 'maxlon', 'maxlat'].forEach(function(name) {
182 .attr('id', 'mapnik_' + name)
184 .attr('type', 'hidden')
189 .attr('name', 'format')
190 .attr('value', 'mapnik')
191 .attr('type', 'hidden')
195 .attr('class', 'deemphasize')
196 .html(I18n.t('javascripts.share.image_size') + ' <span id="mapnik_image_width"></span> x <span id="mapnik_image_height"></span>')
200 .attr('type', 'submit')
201 .attr('value', I18n.t('javascripts.share.download'))
205 .on('change', update)
208 marker.on('dragend', movedMarker);
209 map.on('move', movedMap);
210 map.on('moveend layeradd layerremove', update);
212 options.sidebar.addPane($ui);
218 map.removeLayer(marker);
219 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
220 locationFilter.disable();
228 $('#mapnik_scale').val(getScale());
229 marker.setLatLng(map.getCenter());
232 options.sidebar.togglePane($ui, button);
235 function toggleMarker() {
236 if ($(this).is(':checked')) {
237 marker.setLatLng(map.getCenter());
238 map.addLayer(marker);
239 map.options.scrollWheelZoom = map.options.doubleClickZoom = 'center';
241 map.removeLayer(marker);
242 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
247 function toggleFilter() {
248 if ($(this).is(':checked')) {
249 locationFilter.setBounds(map.getBounds().pad(-0.2));
250 locationFilter.enable();
252 locationFilter.disable();
257 function movedMap() {
258 marker.setLatLng(map.getCenter());
262 function movedMarker() {
263 if (map.hasLayer(marker)) {
264 map.off('move', movedMap);
265 map.on('moveend', updateOnce);
266 map.panTo(marker.getLatLng());
270 function updateOnce() {
271 map.off('moveend', updateOnce);
272 map.on('move', movedMap);
277 var bounds = map.getBounds();
280 .prop('checked', map.hasLayer(marker));
283 .prop('checked', locationFilter.isEnabled());
287 $('#short_input').val(map.getShortUrl(marker));
288 $('#long_input').val(map.getUrl(marker));
289 $('#short_link').attr('href', map.getShortUrl(marker));
290 $('#long_link').attr('href', map.getUrl(marker));
293 bbox: bounds.toBBoxString(),
294 layer: map.getMapBaseLayerId()
297 if (map.hasLayer(marker)) {
298 var latLng = marker.getLatLng().wrap();
299 params.marker = latLng.lat + ',' + latLng.lng;
302 $('#embed_html').val(
303 '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' +
304 escapeHTML('http://' + OSM.SERVER_URL + '/export/embed.html?' + $.param(params)) +
305 '" style="border: 1px solid black"></iframe><br/>' +
306 '<small><a href="' + escapeHTML(map.getUrl(marker)) + '">' +
307 escapeHTML(I18n.t('javascripts.share.view_larger_map')) + '</a></small>');
311 if (locationFilter.isEnabled()) {
312 bounds = locationFilter.getBounds();
315 var scale = $("#mapnik_scale").val(),
316 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
317 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
318 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
320 $('#mapnik_minlon').val(bounds.getWest());
321 $('#mapnik_minlat').val(bounds.getSouth());
322 $('#mapnik_maxlon').val(bounds.getEast());
323 $('#mapnik_maxlat').val(bounds.getNorth());
325 if (scale < maxScale) {
326 scale = roundScale(maxScale);
327 $("#mapnik_scale").val(scale);
330 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
331 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
338 function getScale() {
339 var bounds = map.getBounds(),
340 centerLat = bounds.getCenter().lat,
341 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
342 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
343 pixelsPerMeter = map.getSize().x / meters,
344 metersPerPixel = 1 / (92 * 39.3701);
345 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
348 function roundScale(scale) {
349 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
350 return precision * Math.ceil(scale / precision);
353 return $container[0];