Support Forum
Support › Meta Box Geolocation › Open Street Map and Bricks. Code r doesn't workResolved
Hi
I'm trying to display an OpenStreetMap from a Metabox group (cloneable) following the documentation from https://docs.metabox.io/fields/osm/#outputting-a-map-in-a-group
It returns the following error:
Notice: Undefined index: contact_map in /home/xxxxx/xxxxx/wp-content/themes/bricks/includes/elements/code.php(159) : eval()'d code on line 9
The following code is executed in a Bricks template:
$args = [
'width' => '640px',
'height' => '480px',
];
$group_values = rwmb_meta( 'group_contacts' );
// If group is cloneable
foreach ( $group_values as $group_value ) {
echo RWMB_Map_Field::render_map( $group_value['contact_map'], $args );
}
All the other variables from teh same group are correctly displayed by using {mb_palais_group_contacts_[my id]} in a texte element.
What did I did missed?
I'm having a problem as well using the above code. In my case the maps display briefly then bring up a google error:
Oops! Something went wrong.
This page didn't load Google Maps correctly. See the JavaScript console for technical details.
Google shouldn’t have any concern with this, that’s why I was using Open Streets Map.
Hopefully when you assist Shemzone it will also give me a bump in the right direction.
Hello there,
@Shemzone: can you please add the code to a template file of a theme and recheck the issue? I think there is a compatibility issue when executing the code in the Bricks builder.
@Dan: please create another topic and post your issue there. Make sure that you have the latest version of all extensions on your site. If you are using Google Map field, the issue mostly relates to the validity of your API key.
Hi @Peter and thank you for your attention to my problem.
I'm a bit stuck in the process to test the code.
Do I need to export the fields by following the documentation from https://docs.metabox.io/extensions/meta-box-builder/#getting-php-code and paste it in my functions.php? If yes, what next?
Or do I need only test this code in a PHP file?
If yes, how to pass the post (slug? id?) value to render the custom fields values?
<?php
$args = [
'width' => '640px',
'height' => '480px',
];
$group_values = rwmb_meta( 'group_contacts' );
// If group is cloneable
foreach ( $group_values as $group_value ) {
echo RWMB_Map_Field::render_map( $group_value['contact_map'], $args );
}
?>
I can't really bypass bricks theme because if I do (chosing another theme and create a template), it brokes all the website. I did and ahd a hard time to get it all back.
Ok, you can get the PHP code from the builder and share it here. I will check if there is something wrong with the group and subfield ID.
Regarding the third parameter that needs to pass to the helper function as the post (object ID). You can read more on this documentation https://docs.metabox.io/functions/rwmb-meta/
For example:
$group_values = rwmb_meta( 'group_contacts', '', 123 );
Where 123 is the post ID.
Hi again @Peter
I did a test directly from a PHP file using the following code, same result:
echo '<h1>Test OSM Maps</h1>';
$args = [
'width' => '640px',
'height' => '480px',
'zoom' => 14,
'marker' => true
];
$group_values = rwmb_meta( 'group_contacts', '', 204 );
// If group is cloneable
foreach ( $group_values as $group_value ) {
echo RWMB_Map_Field::render_map( $group_value['contact_map'], $args );
echo '--------------';
}
The weird part is the maps display for 1 second and are immediatly replaced by a Google Maps message. See the screen record https://www.loom.com/share/e8782b7add2b4038800d970cef7a8bf9
Here his the fields PHP code:
add_filter( 'rwmb_meta_boxes', 'test_mb_palais' );
function test_mb_palais( $meta_boxes ) {
$prefix = 'palais-';
$meta_boxes[] = [
'title' => __( 'Palais', 'your-text-domain' ),
'id' => 'palais',
'post_types' => ['palais'],
'fields' => [
[
'name' => __( 'Sous-titre', 'your-text-domain' ),
'id' => $prefix . 'text_subtitle',
'type' => 'text',
],
[
'type' => 'heading',
'name' => __( 'Comment y aller', 'your-text-domain' ),
],
[
'name' => __( 'En train', 'your-text-domain' ),
'id' => $prefix . 'textarea_train',
'type' => 'wysiwyg',
],
[
'name' => __( 'En avion', 'your-text-domain' ),
'id' => $prefix . 'textarea_avion',
'type' => 'wysiwyg',
],
[
'name' => __( 'En voiture', 'your-text-domain' ),
'id' => $prefix . 'textarea_voiture',
'type' => 'wysiwyg',
],
[
'name' => __( 'Juridictions', 'your-text-domain' ),
'id' => $prefix . 'textarea_juridictions',
'type' => 'wysiwyg',
],
[
'name' => __( 'À découvrir', 'your-text-domain' ),
'id' => $prefix . 'textarea_discover',
'type' => 'wysiwyg',
],
[
'type' => 'heading',
'name' => __( 'Contacts et adresses', 'your-text-domain' ),
],
[
'name' => __( 'Contacts', 'your-text-domain' ),
'id' => $prefix . 'group_contacts',
'type' => 'group',
'collapsible' => true,
'default_state' => 'collapsed',
'clone' => true,
'sort_clone' => true,
'add_button' => __( 'Ajouter une adresse', 'your-text-domain' ),
'fields' => [
[
'name' => __( 'Intitulé', 'your-text-domain' ),
'id' => $prefix . 'contact_title',
'type' => 'text',
],
[
'name' => __( 'Adresse', 'your-text-domain' ),
'id' => $prefix . 'contact_address',
'type' => 'text',
],
[
'name' => __( 'Téléphone', 'your-text-domain' ),
'id' => $prefix . 'contact_phone',
'type' => 'text',
],
[
'name' => __( 'Email', 'your-text-domain' ),
'id' => $prefix . 'contact_email',
'type' => 'email',
],
[
'name' => __( 'Horaires', 'your-text-domain' ),
'id' => $prefix . 'contact_opening_hours',
'type' => 'wysiwyg',
],
[
'name' => __( 'Transports en commun', 'your-text-domain' ),
'id' => $prefix . 'contact_public_transportation',
'type' => 'wysiwyg',
],
[
'name' => __( 'Carte', 'your-text-domain' ),
'id' => $prefix . 'contact_map',
'type' => 'osm',
'address_field' => 'contact_address',
'language' => 'fr',
'region' => 'fr',
],
],
],
],
];
return $meta_boxes;
}
@Peter
in addition, here is the console log (seem to expect a Google API Key Map, that I don't use):
Google Maps JavaScript API error: InvalidKeyMapError js?key=AIzaSyC1mUh87SGFyf133tpZQJa-s96p0tgnraQ&libraries=places&ver=5.6.7:91
https://developers.google.com/maps/documentation/javascript/error-messages#invalid-key-map-error
_.pe @ js?key=AIzaSyC1mUh87SGFyf133tpZQJa-s96p0tgnraQ&libraries=places&ver=5.6.7:91
(anonyme) @ common.js:137
(anonyme) @ common.js:258
c @ common.js:122
Please change the class RWMB_Map_Field
to this one RWMB_OSM_Field
, it is a typo issue on the documentation and it will be fixed as soon as possible. The code should be
echo RWMB_OSM_Field::render_map( $group_value['osm'], $args );