No default map

  • Hi,
    I’m working on a website that had an ACF data type that includes GPS coordinates; and on the single page for that data type, I am displaying that map using the ACF Map field type (with dynamic data – {{post_meta key:map}}). I don’t want this map to show up if I don’t enter in the GPS coordinates, and it’s set to require value to render, but the map seems to have a default location. Is there a way to fix this with Generate Press, or should I contact the ACF support? I’ll include a link to a page below, where the sign doesn’t have location data attached to it (but the map is still showing up).
    Thanks!
    Elisabeth

  • Hi Elisabeth,

    If the field has a default value, then it’s not something that GB can fix.

    You will need to check with ACF’s support on this, ask them how to remove the defualt value.

  • Ok thanks, I will do that. In case they can’t help, would there be a way to make the map not show up if a certain set of values (i.e. in the ocean, 0,0 GPS coordinates) are inputted? Maybe through a PHP function?

    Thanks,
    Elisabeth

  • Hello,

    ACF only seems to have a Google Map field, is that what you are using?

  • No, I’m using the openstreet map field.

  • I am not able to display the map in my local install. Please provide details with your ACF settings. Is it this plugin you are using?

  • I’m using ACF’s field type called OpenStreetMap and yes, that’s the plugin I’m using.
    https://wordpress.org/plugins/acf-openstreetmap-field/
    If you want to check it out on the back end, I included login info below, as well as a screenshot of the ACF field group.

  • Hi Elisabeth,

    After some investigation, the issue is that the ACF OpenStreetMap Field plugin retains the map center coordinates in the database even when the field is cleared — so GB’s “require value to render” sees a non-empty value and renders the map regardless.

    The fix involves two parts: a custom GB Pro display condition that checks whether the map field has a valid location, and a small snippet that clears the meta when the field is saved without a marker.

    Add the following to your child theme’s functions.php or a code snippets plugin:

    class GB_OSM_Map_Condition extends GenerateBlocks_Pro_Condition_Abstract {
    
        public function evaluate( $rule, $operator, $value, $context = [] ) {
            switch ( $rule ) {
                case 'osm_has_location':
                    $post_id = $context['post_id'] ?? get_the_ID();
                    wp_cache_delete( $post_id, 'post_meta' );
                    $map = get_post_meta( $post_id, 'map', true );
                    $has_location = ! empty( $map ) && is_array( $map ) && ! empty( $map['lat'] );
                    return 'is' === $operator ? $has_location : ! $has_location;
                default:
                    return false;
            }
        }
    
        public function get_rules() {
            return [
                'osm_has_location' => __( 'OSM Map Has Location', 'generateblocks' ),
            ];
        }
    
        public function get_rule_metadata( $rule ) {
            return [
                'needs_value'    => true,
                'value_type'     => 'text',
                'supports_multi' => false,
            ];
        }
    
        public function sanitize_value( $value, $rule ) {
            return sanitize_text_field( $value );
        }
    }
    
    add_action( 'generateblocks_register_conditions', function() {
        GenerateBlocks_Pro_Conditions_Registry::register(
            'osm_map',
            [
                'label'     => __( 'OSM Map', 'generateblocks' ),
                'operators' => [ 'is', 'is_not' ],
                'priority'  => 100,
            ],
            'GB_OSM_Map_Condition'
        );
    } );
    
    add_action( 'acf/save_post', function( $post_id ) {
        $field_key = 'YOUR_FIELD_KEY';
        $raw = $_POST['acf'][ $field_key ] ?? '';
    
        if ( empty( $raw ) ) {
            update_post_meta( $post_id, 'map', '' );
            return;
        }
    
        $data = json_decode( stripslashes( $raw ), true );
    
        if ( empty( $data['markers'] ) ) {
            update_post_meta( $post_id, 'map', '' );
        }
    }, 20 );

    Once the code is in place, edit the map block in your GP template and add a Display Condition: OSM Map → OSM Map Has Location → is, with map as the value. The block will then only render when a valid location has been saved.

    Map condition

    I have tried this solution locally and it worked. If it doesn’t work for you, you would need to get in touch with the plugin developer and point out this bug.

  • That worked – thank you so much George!

  • No problem!

  • Hi George,
    I just noticed that the map field no longer seems to be saving map locations when I enter gps coordinates for new signs. The ones that existed before I added the code are still showing up, but none after that. Is there something that needs to change in order to add a location to a sign?

  • Hi,

    How do you enter the coordinates? For example, when I enter coordinates for London: 51.5074, -0.1278, then click the magnify button to search for them, the location is found. When I save the post, I can see the map with the new coordinates in the front end.

  • I enter the coordinates into the map field (which is a field in the sign details field group), for example, “34°3’49” N 4°58’21” W” and they show up. When I update or save the post, the coordinates are gone, and nothing shows up on the back end. I’ll include the link below for the post with the coordinates 34°3’49” N 4°58’21” W.
    Elisabeth

  • When I update or save the post, the coordinates are gone,

    Do you mean the coordinates are gone from the field?

  • Yes, the coordinates are gone and it’s reset to 0, 0.

  • It sounds like an issue with the field plugin itself.

    Can you remove George’s code and test it again?

Viewing 16 posts - 1 through 16 (of 24 total)
  • You must be logged in to reply to this topic.