Support

Account

Home Forums ACF PRO Google Maps crashing when no location data passed

Solved

Google Maps crashing when no location data passed

  • So when pulling locations from a CPT and one of the CPT items is lacking location info – ACF + Google maps chokes hard and stops loading the rest of the page. Console dumps the following:

    
    Uncaught Error: Syntax error, unrecognized expression: [data-lat='<br />
    <b>Warning</b>:  Illegal string offset 'lat' in <b>[...]page-map.php</b> on line <b>28</b><br />
    ']
    

    line 28 is just the standard marker details:

    
    <div class="marker" data-lat="<?php echo $space_address['lat']; ?>" data-lng="<?php echo $space_address['lng']; ?>">
    

    Is this just something going wrong with my config? or is there a way to just have ACF/Maps passover / not display items with location info?

    It might be useful to know that i’m calling the map data twice – once to draw the Google Map + Markers, and another time for an external nav, where i’ve got a list of the map marker locations that can be clicked to open and close marker locations. Is there just no failsafe code written for empty markers outside of the map perhaps? If so – how can i go about fixing the issue.

    Thanks!

  • Update for anyone reading this – the inclusion of an

    
    if( !empty($location) ):
    

    wrapper around the markers solves the issue.

  • Hi,

    I’m having the same issue, can you guide me where to include the above code in my file? Thanks in advance!

    <?php

    while ( have_posts() ) : the_post();

    $location = get_field(‘map’);
    $lat = $location[‘lat’]; <—-Illegal string offset ‘lat’
    $lng = $location[‘lng’]; <—-Illegal string offset ‘lng’
    $coords = $lat.”,”.$lng;
    $post_counter++;
    if (strlen($lat) > 1) {

    echo ‘[“<a href=’;
    echo the_permalink();
    echo “/’><h2>”;
    echo the_title();
    echo “</h2><div class=’impact’><div class=’address’><address>”;
    echo the_field( ‘address_1’);
    echo ‘</br>’;
    echo the_field( ‘city’);
    echo ‘ ‘;
    echo the_field( ‘state’);
    echo ‘ ‘;
    echo the_field( ‘zip’);
    echo ‘</address></div>’;
    echo ‘</div>”, ‘;
    echo $coords;
    echo ‘, “/wp-content/themes/ci_old/map/marker’;
    echo $post_counter;
    echo ‘.png”,’;
    echo $post_counter ;
    echo ‘],’;
    }
    endwhile;
    ?>

  • You need to check to see if the lat and lng indexes are set on the field array. You do this using the isset() PHP method. If they aren’t shown you can display an error (or nothing) and if they are then proceed with the rest of your code.

    
    $location = get_field('map');
    if ( isset( $location['lat'] ) && isset( $location['lng'] ) ){
        $lat = $location['lat']; 
        $lng = $location['lng'];
        $coords = $lat.".".$lng;
        // ... the rest of your code
    } else {
        // ... the lat/lng location is not set
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Google Maps crashing when no location data passed’ is closed to new replies.