| 1 |
<?php |
| 2 |
/** |
| 3 |
* Image Shortcode |
| 4 |
* |
| 5 |
* Displays map with [leaflet-image src="path/to/image.jpg"] |
| 6 |
* |
| 7 |
* JavaScript equivalent : L.imageOverlay('path/to/image.jpg'); |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
// Exit if accessed directly |
| 12 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 13 |
|
| 14 |
include_once(LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.map-shortcode.php'); |
| 15 |
|
| 16 |
class Leaflet_Image_Shortcode extends Leaflet_Map_Shortcode { |
| 17 |
/** |
| 18 |
* Get HTML for shortcode |
| 19 |
* @param array $atts |
| 20 |
* @return string $content produced by adding atts to JavaScript |
| 21 |
*/ |
| 22 |
protected function getHTML ($atts='', $content=null) { |
| 23 |
extract($this->getAtts($atts)); |
| 24 |
|
| 25 |
/* only required field for image map (src/source) */ |
| 26 |
$src = empty($src) ? '' : $src; |
| 27 |
$source = empty($source) ? 'https://lorempixel.com/1000/1000/' : $source; |
| 28 |
$source = empty($src) ? $source : $src; |
| 29 |
|
| 30 |
ob_start(); |
| 31 |
?> |
| 32 |
<div |
| 33 |
id="leaflet-map-image-<?php echo $this->map_id; ?>" |
| 34 |
class="leaflet-map" |
| 35 |
style="height:<?php echo $height; ?>; width:<?php echo $width; ?>;"></div> |
| 36 |
<script> |
| 37 |
WPLeafletMapPlugin.add(function () { |
| 38 |
var map, |
| 39 |
options = L.Util.extend({}, { |
| 40 |
maxZoom: <?php echo $max_zoom; ?>, |
| 41 |
minZoom: <?php echo $min_zoom; ?>, |
| 42 |
zoomControl: <?php echo $zoomcontrol; ?>, |
| 43 |
scrollWheelZoom: <?php echo $scrollwheel; ?>, |
| 44 |
doubleClickZoom: <?php echo $doubleclickzoom; ?>, |
| 45 |
attributionControl: false |
| 46 |
}, <?php echo $more_options; ?>, { |
| 47 |
crs: L.CRS.Simple |
| 48 |
}), |
| 49 |
image_src = '<?php echo $source; ?>', |
| 50 |
img = new Image(), |
| 51 |
zoom = <?php echo $zoom; ?>; |
| 52 |
img.onload = function() { |
| 53 |
var h = img.height, |
| 54 |
w = img.width, |
| 55 |
projected_zoom = zoom + 1, |
| 56 |
southWest = map.unproject([-w, h], projected_zoom), |
| 57 |
northEast = map.unproject([w, -h], projected_zoom), |
| 58 |
bounds = new L.LatLngBounds(southWest, northEast); |
| 59 |
L.imageOverlay( image_src, bounds ).addTo( map ); |
| 60 |
map.setMaxBounds(bounds); |
| 61 |
}; |
| 62 |
img.src = image_src; |
| 63 |
map = L.map('leaflet-map-image-<?php echo $this->map_id; ?>', options).setView([0, 0], zoom); |
| 64 |
// make it known that it is an image map |
| 65 |
map.is_image_map = true; |
| 66 |
if (<?php echo $fit_markers; ?>) { |
| 67 |
map.fit_markers = true; |
| 68 |
} |
| 69 |
<?php |
| 70 |
if ($attribution) { |
| 71 |
/* add any attributions, semi-colon-separated */ |
| 72 |
$attributions = explode(';', $attribution); |
| 73 |
?> |
| 74 |
var attControl = L.control.attribution({prefix:false}).addTo(map); |
| 75 |
<?php |
| 76 |
foreach ($attributions as $a) { |
| 77 |
?> |
| 78 |
attControl.addAttribution('<?php echo trim($a); ?>'); |
| 79 |
<?php |
| 80 |
} |
| 81 |
} |
| 82 |
?> |
| 83 |
WPLeafletMapPlugin.maps.push( map ); |
| 84 |
WPLeafletMapPlugin.images.push( img ); |
| 85 |
}); // end add |
| 86 |
</script> |
| 87 |
<?php |
| 88 |
return ob_get_clean(); |
| 89 |
} |
| 90 |
} |
| 91 |
|