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', 'Share')
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 $shortLink, $longLink;
50 .appendTo($linkSection)
52 .append($longLink = $('<a>')
53 .text(I18n.t('javascripts.share.long_link'))))
55 .append($shortLink = $('<a>')
56 .text(I18n.t('javascripts.share.short_link'))));
60 var $embedSection = $('<div>')
61 .attr('class', 'section share-html')
65 .text(I18n.t('javascripts.share.embed'))
66 .appendTo($embedSection);
68 var $form = $('<form>')
69 .attr('class', 'standard-form')
70 .attr('action', '/export/finish')
71 .attr('method', 'post')
72 .appendTo($embedSection);
75 .attr('class', 'form-row')
79 .attr('for', 'embed_marker')
82 .attr('id', 'embed_marker')
83 .attr('type', 'checkbox')
84 .bind('change', toggleMarker))
85 .append(I18n.t('javascripts.share.include_marker')))
88 .attr('class', 'form-row')
92 .attr('id', 'embed_html')
93 .on('click', select));
96 .attr('class', 'deemphasize')
97 .text(I18n.t('javascripts.share.paste_html'))
98 .appendTo($embedSection);
102 var $imageSection = $('<div>')
103 .attr('class', 'section share-image')
107 .text(I18n.t('javascripts.share.image'))
108 .appendTo($imageSection);
111 .attr('class', 'standard-form')
112 .attr('action', '/export/finish')
113 .attr('method', 'post')
114 .appendTo($imageSection);
117 .attr('class', 'form-row')
121 .attr('for', 'image_filter')
124 .attr('id', 'image_filter')
125 .attr('type', 'checkbox')
126 .bind('change', toggleFilter))
127 .append(I18n.t('javascripts.share.custom_dimensions')));
130 .attr('class', 'form-row')
134 .attr('class', 'standard-label')
135 .attr('for', 'mapnik_format')
136 .text(I18n.t('javascripts.share.format')))
137 .append($('<select>')
138 .attr('name', 'mapnik_format')
139 .attr('id', 'mapnik_format')
140 .append($('<option>').val('png').text('PNG').prop('selected', true))
141 .append($('<option>').val('jpeg').text('JPEG'))
142 .append($('<option>').val('svg').text('SVG'))
143 .append($('<option>').val('pdf').text('PDF')));
146 .attr('class', 'form-row')
149 .attr('class', 'standard-label')
150 .attr('for', 'mapnik_scale')
151 .text(I18n.t('javascripts.share.scale')))
154 .attr('name', 'mapnik_scale')
155 .attr('id', 'mapnik_scale')
156 .attr('type', 'text')
157 .on('change', update));
159 ['minlon', 'minlat', 'maxlon', 'maxlat'].forEach(function(name) {
161 .attr('id', 'mapnik_' + name)
163 .attr('type', 'hidden')
168 .attr('name', 'format')
169 .attr('value', 'mapnik')
170 .attr('type', 'hidden')
174 .attr('class', 'deemphasize')
175 .html(I18n.t('javascripts.share.image_size') + ' <span id="mapnik_image_width"></span> x <span id="mapnik_image_height"></span>')
179 .attr('type', 'submit')
180 .attr('value', I18n.t('javascripts.share.download'))
184 .on('change', update)
187 map.on('moveend layeradd layerremove', update);
189 options.sidebar.addPane($ui);
195 $('#mapnik_scale').val(getScale());
196 marker.setLatLng(map.getCenter());
199 options.sidebar.togglePane($ui);
202 function toggleMarker() {
203 if ($(this).is(':checked')) {
204 map.addLayer(marker);
206 map.removeLayer(marker);
211 function toggleFilter() {
212 if ($(this).is(':checked')) {
213 if (!locationFilter.getBounds().isValid()) {
214 locationFilter.setBounds(map.getBounds().pad(-0.2));
217 locationFilter.enable();
219 locationFilter.disable();
227 $shortLink.attr('href', map.getShortUrl());
228 $longLink.attr('href', map.getUrl());
232 var bounds = map.getBounds(),
233 center = bounds.getCenter(),
235 bbox: bounds.toBBoxString(),
236 layer: map.getMapBaseLayerId()
241 zoom: map.getBoundsZoom(bounds),
242 layers: map.getLayersCode()
245 if (map.hasLayer(marker)) {
246 var m = marker.getLatLng();
247 params.marker = m.lat + ',' + m.lng;
248 linkParams.mlat = m.lat;
249 linkParams.mlon = m.lng;
252 $('#embed_html').val(
253 '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' +
254 'http://' + OSM.SERVER_URL + '/export/embed.html?' + $.param(params) +
255 '" style="border: 1px solid black"></iframe><br/>' +
257 'http://' + OSM.SERVER_URL + '/?' + $.param(linkParams) +
258 '">' + I18n.t('export.start_rjs.view_larger_map') + '</a></small>');
262 if (locationFilter.isEnabled()) {
263 bounds = locationFilter.getBounds();
266 var scale = $("#mapnik_scale").val(),
267 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
268 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
269 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
271 $('#mapnik_minlon').val(bounds.getWest());
272 $('#mapnik_minlat').val(bounds.getSouth());
273 $('#mapnik_maxlon').val(bounds.getEast());
274 $('#mapnik_maxlat').val(bounds.getNorth());
276 if (scale < maxScale) {
277 scale = roundScale(maxScale);
278 $("#mapnik_scale").val(scale);
281 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
282 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
289 function getScale() {
290 var bounds = map.getBounds(),
291 centerLat = bounds.getCenter().lat,
292 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
293 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
294 pixelsPerMeter = map.getSize().x / meters,
295 metersPerPixel = 1 / (92 * 39.3701);
296 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
299 function roundScale(scale) {
300 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
301 return precision * Math.ceil(scale / precision);
304 return $container[0];