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')
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', 'sidebar_close')
32 .bind('click', toggle))
35 .text(I18n.t('javascripts.share.title')));
39 var $linkSection = $('<div>')
40 .attr('class', 'section share-link')
44 .text(I18n.t('javascripts.share.link'))
45 .appendTo($linkSection);
47 var $form = $('<form>')
48 .attr('class', 'standard-form')
49 .appendTo($linkSection);
52 .attr('class', 'form-row')
56 .attr('for', 'link_marker')
59 .attr('id', 'link_marker')
60 .attr('type', 'checkbox')
61 .bind('change', toggleMarker))
62 .append(I18n.t('javascripts.share.include_marker')));
65 .attr('class', 'share-tabs')
68 .attr('class', 'active')
69 .attr('for', 'long_input')
70 .attr('id', 'long_link')
71 .text(I18n.t('javascripts.share.long_link')))
73 .attr('for', 'short_input')
74 .attr('id', 'short_link')
75 .text(I18n.t('javascripts.share.short_link')))
77 .attr('for', 'embed_html')
79 .text(I18n.t('javascripts.share.embed')))
80 .on('click', 'a', function(e) {
82 var id = '#' + $(this).attr('for');
83 $linkSection.find('.share-tabs a')
84 .removeClass('active');
85 $(this).addClass('active');
86 $linkSection.find('.share-tab')
88 $linkSection.find('.share-tab:has(' + id + ')')
90 .find('input, textarea')
95 .attr('class', 'form-row share-tab')
96 .css('display', 'block')
99 .attr('id', 'long_input')
100 .attr('type', 'text')
101 .on('click', select));
104 .attr('class', 'form-row share-tab')
107 .attr('id', 'short_input')
108 .attr('type', 'text')
109 .on('click', select));
112 .attr('class', 'form-row share-tab')
116 .attr('id', 'embed_html')
117 .on('click', select))
120 .attr('class', 'deemphasize')
121 .text(I18n.t('javascripts.share.paste_html'))
122 .appendTo($linkSection));
126 var $imageSection = $('<div>')
127 .attr('class', 'section share-image')
131 .text(I18n.t('javascripts.share.image'))
132 .appendTo($imageSection);
135 .attr('class', 'standard-form')
136 .attr('action', '/export/finish')
137 .attr('method', 'post')
138 .appendTo($imageSection);
141 .attr('class', 'form-row')
145 .attr('for', 'image_filter')
148 .attr('id', 'image_filter')
149 .attr('type', 'checkbox')
150 .bind('change', toggleFilter))
151 .append(I18n.t('javascripts.share.custom_dimensions')));
154 .attr('class', 'form-row')
158 .attr('for', 'mapnik_format')
159 .text(I18n.t('javascripts.share.format')))
160 .append($('<select>')
161 .attr('name', 'mapnik_format')
162 .attr('id', 'mapnik_format')
163 .append($('<option>').val('png').text('PNG').prop('selected', true))
164 .append($('<option>').val('jpeg').text('JPEG'))
165 .append($('<option>').val('svg').text('SVG'))
166 .append($('<option>').val('pdf').text('PDF')));
169 .attr('class', 'form-row')
172 .attr('for', 'mapnik_scale')
173 .text(I18n.t('javascripts.share.scale')))
176 .attr('name', 'mapnik_scale')
177 .attr('id', 'mapnik_scale')
178 .attr('type', 'text')
179 .on('change', update));
181 ['minlon', 'minlat', 'maxlon', 'maxlat'].forEach(function(name) {
183 .attr('id', 'mapnik_' + name)
185 .attr('type', 'hidden')
190 .attr('name', 'format')
191 .attr('value', 'mapnik')
192 .attr('type', 'hidden')
196 .attr('class', 'deemphasize')
197 .html(I18n.t('javascripts.share.image_size') + ' <span id="mapnik_image_width"></span> x <span id="mapnik_image_height"></span>')
201 .attr('type', 'submit')
202 .attr('value', I18n.t('javascripts.share.download'))
206 .on('change', update)
209 marker.on('dragend', movedMarker);
210 map.on('move', movedMap);
211 map.on('moveend layeradd layerremove', update);
213 options.sidebar.addPane($ui);
219 map.removeLayer(marker);
220 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
221 locationFilter.disable();
229 $('#mapnik_scale').val(getScale());
230 marker.setLatLng(map.getCenter());
233 options.sidebar.togglePane($ui, button);
236 function toggleMarker() {
237 if ($(this).is(':checked')) {
238 marker.setLatLng(map.getCenter());
239 map.addLayer(marker);
240 map.options.scrollWheelZoom = map.options.doubleClickZoom = 'center';
242 map.removeLayer(marker);
243 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
248 function toggleFilter() {
249 if ($(this).is(':checked')) {
250 locationFilter.setBounds(map.getBounds().pad(-0.2));
251 locationFilter.enable();
253 locationFilter.disable();
258 function movedMap() {
259 marker.setLatLng(map.getCenter());
263 function movedMarker() {
264 if (map.hasLayer(marker)) {
265 map.off('move', movedMap);
266 map.on('moveend', updateOnce);
267 map.panTo(marker.getLatLng());
271 function updateOnce() {
272 map.off('moveend', updateOnce);
273 map.on('move', movedMap);
278 var bounds = map.getBounds();
281 .prop('checked', map.hasLayer(marker));
284 .prop('checked', locationFilter.isEnabled());
288 $('#short_input').val(map.getShortUrl(marker));
289 $('#long_input').val(map.getUrl(marker));
290 $('#short_link').attr('href', map.getShortUrl(marker));
291 $('#long_link').attr('href', map.getUrl(marker));
294 bbox: bounds.toBBoxString(),
295 layer: map.getMapBaseLayerId()
298 if (map.hasLayer(marker)) {
299 var latLng = marker.getLatLng().wrap();
300 params.marker = latLng.lat + ',' + latLng.lng;
303 $('#embed_html').val(
304 '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' +
305 escapeHTML('http://' + OSM.SERVER_URL + '/export/embed.html?' + $.param(params)) +
306 '" style="border: 1px solid black"></iframe><br/>' +
307 '<small><a href="' + escapeHTML(map.getUrl(marker)) + '">' +
308 escapeHTML(I18n.t('javascripts.share.view_larger_map')) + '</a></small>');
312 if (locationFilter.isEnabled()) {
313 bounds = locationFilter.getBounds();
316 var scale = $("#mapnik_scale").val(),
317 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
318 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
319 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
321 $('#mapnik_minlon').val(bounds.getWest());
322 $('#mapnik_minlat').val(bounds.getSouth());
323 $('#mapnik_maxlon').val(bounds.getEast());
324 $('#mapnik_maxlat').val(bounds.getNorth());
326 if (scale < maxScale) {
327 scale = roundScale(maxScale);
328 $("#mapnik_scale").val(scale);
331 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
332 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
339 function getScale() {
340 var bounds = map.getBounds(),
341 centerLat = bounds.getCenter().lat,
342 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
343 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
344 pixelsPerMeter = map.getSize().x / meters,
345 metersPerPixel = 1 / (92 * 39.3701);
346 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
349 function roundScale(scale) {
350 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
351 return precision * Math.ceil(scale / precision);
354 return $container[0];