Skip to content

Commit 10511ea

Browse files
committed
Added CMB2_MB_Map, map center fallbacks, better array key checks, and option to set default editor zoom
1 parent db37aa5 commit 10511ea

File tree

2 files changed

+160
-6
lines changed

2 files changed

+160
-6
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ Array
2121
)
2222
```
2323

24+
To add a maxbox_map field:
25+
26+
```
27+
$cmb2->add_field(
28+
array(
29+
'name' => __( 'Map Location', 'mtsi' ),
30+
'desc' => __( 'Drop a pin for this location.', 'mtsi' ),
31+
'id' => '_my_meta_key',
32+
'type' => 'mapbox_map',
33+
)
34+
);
35+
```
36+
37+
Optionally, you can set the default zoom for the map (used when loaded with no previously-set location):
38+
39+
```
40+
$cmb2->add_field(
41+
array(
42+
'name' => __( 'Map Location', 'mtsi' ),
43+
'desc' => __( 'Drop a pin for this location.', 'mtsi' ),
44+
'id' => '_my_meta_key',
45+
'type' => 'mapbox_map',
46+
'default_zoom' => 3,
47+
)
48+
);
49+
```
50+
51+
A PHP class, CMB2_MB_Map, has also been provided for building maps with pins from multiple posts. Documentation coming shortly.
52+
2453
## Installation
2554

2655
1. Upload the `cmb2-mapbox` folder to the `/wp-content/plugins/` directory

cmb2-mapbox.php

+131-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: CMB2 Mapbox
44
* Plugin URI:
55
* Description: This plugin adds a new CMB2 fieldtype for adding a single point to a Mapbox map. This plugin requires CMB2 and a Mapbox access token.
6-
* Version: 1.0.3
6+
* Version: 1.5.0
77
* Author: Rob Clark
88
* Author URI: https://robclark.io
99
* License: GPLv2 or later
@@ -59,11 +59,11 @@ function cmb2_mapbox_options_metabox() {
5959

6060

6161
function cmb2_mapbox_scripts() {
62-
wp_enqueue_style( 'mapbox-gl', 'https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css', array(), null );
63-
wp_enqueue_script( 'mapbox-gl', 'https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.js', array(), null );
62+
wp_enqueue_style( 'mapbox-gl', 'https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.css', array(), null );
63+
wp_enqueue_script( 'mapbox-gl', 'https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.js', array(), null );
6464
if ( is_admin() ) {
65-
wp_enqueue_style( 'mapbox-gl-draw', 'https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.2/mapbox-gl-draw.css', array(), null );
66-
wp_enqueue_script( 'mapbox-gl-draw', 'https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.2/mapbox-gl-draw.js', array( 'mapbox-gl' ), null );
65+
wp_enqueue_style( 'mapbox-gl-draw', 'https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.4.1/mapbox-gl-draw.css', array(), null );
66+
wp_enqueue_script( 'mapbox-gl-draw', 'https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.4.1/mapbox-gl-draw.js', array( 'mapbox-gl' ), null );
6767
}
6868
}
6969

@@ -93,7 +93,8 @@ function cmb2_render_mapbox_map_callback( $field, $value, $object_id, $object_ty
9393
zoom: 13,
9494
center: [<?php echo $value['lnglat']; ?>]
9595
<?php } else { ?>
96-
zoom: 11<?php echo ( isset( $options['map_center'] ) ? ',center: [' . $options['map_center'] . ']' : '' ); ?>
96+
zoom: <?php echo ( cmb2_mapbox_check_array_key( $field->args, 'default_zoom' ) ? $field->args['default_zoom'] : '11' ); ?>,
97+
center: [<?php echo ( cmb2_mapbox_check_array_key( $options, 'map_center' ) ? $options['map_center'] : '-95.7129,37.0902' ); ?>]
9798
<?php } ?>
9899
});
99100
map.addControl(new mapboxgl.NavigationControl());
@@ -216,3 +217,127 @@ function updateEditor(e) {
216217
}
217218

218219
add_filter( 'cmb2_render_mapbox_map', 'cmb2_render_mapbox_map_callback', 10, 5 );
220+
221+
function cmb2_mapbox_check_array_key( $item, $key ) {
222+
$output = false;
223+
if ( is_array( $item ) ) {
224+
if ( array_key_exists( $key, $item ) ) {
225+
if ( ! empty( $item["{$key}"] ) ) {
226+
$output = true;
227+
}
228+
}
229+
}
230+
return $output;
231+
}
232+
233+
if ( ! class_exists( 'CMB2_MB_Map' ) ) {
234+
class CMB2_MB_Map {
235+
protected $plugin_options = array();
236+
protected $map_options = array();
237+
protected $geo = array();
238+
239+
function __construct() {
240+
$this->set_options();
241+
242+
// Set up arrays for geo
243+
$this->geo['markers'] = array();
244+
}
245+
246+
private function set_options() {
247+
// Set plugin options
248+
$defaults = array(
249+
'api_token' => '',
250+
'map_center' => '-95.7129,37.0902',
251+
);
252+
$this->plugin_options = wp_parse_args( get_option( 'cmb2_mapbox' ), $defaults );
253+
}
254+
255+
public function set_map_options( $args ) {
256+
$defaults = array(
257+
'id' => 'map',
258+
'class' => 'cmb2-mapbox-map',
259+
'width' => '100%',
260+
'height' => '400px',
261+
'zoom' => '16',
262+
'center' => '-95.7129,37.0902',
263+
'mapstyle' => 'mapbox://styles/mapbox/streets-v11',
264+
'terrain' => false,
265+
);
266+
$this->map_options = wp_parse_args( $args, $defaults );
267+
}
268+
269+
public function add_marker( $geo, $tooltip, $color ) {
270+
$this->geo['markers'][] = array(
271+
'geo' => $geo,
272+
'tooltip' => $tooltip,
273+
'color' => $color,
274+
);
275+
return;
276+
}
277+
278+
public function build_map() {
279+
$output = '';
280+
$marker_html = '';
281+
$markers = array();
282+
if ( 0 < count( $this->geo['markers'] ) ) {
283+
$i = 1;
284+
foreach( $this->geo['markers'] as $marker ) {
285+
$html = '
286+
const popup' . $i . ' = new mapboxgl.Popup({ offset: 25 }).setMaxWidth("300px").setHTML(
287+
\'' . '<div class="mapbox-popup-content-wrap">' . $marker['tooltip'] . '</div>\'
288+
);
289+
290+
const marker' . $i . ' = new mapboxgl.Marker({ color: \'' . $marker['color'] . '\' })
291+
.setLngLat([' . $marker['geo']['lnglat'] . '])
292+
.setPopup(popup' . $i . ')
293+
.addTo(map);
294+
';
295+
$markers[] = array(
296+
'html' => $html,
297+
'lat' => $marker['geo']['lat']
298+
);
299+
$i++;
300+
}
301+
array_multisort( array_column( $markers, 'lat' ), SORT_DESC, $markers );
302+
foreach ($markers as $marker) {
303+
$marker_html .= $marker['html'];
304+
}
305+
$output .= '
306+
<div id="' . $this->map_options['id'] . '" class="' . $this->map_options['class'] . '" style="height: ' . $this->map_options['height'] . '; width: ' . $this->map_options['width'] . ';"></div>
307+
<script>
308+
mapboxgl.accessToken = \'' . $this->plugin_options['api_token'] . '\';
309+
var map = new mapboxgl.Map({
310+
container: \'' . $this->map_options['id'] . '\',
311+
style: \'' . $this->map_options['mapstyle'] . '\',
312+
zoom: ' . $this->map_options['zoom'] . ',
313+
scrollZoom: false,
314+
center: [' . $this->map_options['center'] . '],
315+
});
316+
map.on(\'load\', function () {
317+
var nav = new mapboxgl.NavigationControl();
318+
map.addControl(nav, \'top-left\');
319+
' . $marker_html . '
320+
';
321+
if ( $this->map_options['terrain'] ) {
322+
$output .= '
323+
map.addSource(\'dem\', {
324+
\'type\': \'raster-dem\',
325+
\'url\': \'mapbox://mapbox.terrain-rgb\'
326+
});
327+
map.addLayer({
328+
\'id\': \'hillshading\',
329+
\'source\': \'dem\',
330+
\'type\': \'hillshade\'
331+
},);
332+
333+
';
334+
}
335+
$output .= '
336+
});
337+
</script>
338+
';
339+
}
340+
return $output;
341+
}
342+
}
343+
}

0 commit comments

Comments
 (0)