Support

Account

Home Forums General Issues Google maps with multiple markers

Solving

Google maps with multiple markers

  • I have some code that generates a Google Map to show a marker based on the location field supplied via a custom field named ‘event_map’. It works great.

    <?php 
    $location = get_field('event_map');
    if( !empty($location) ):
    ?>
    <div class="acf-map">
    <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
    </div>
    <?php endif; ?>

    I now want to add a map that shows ALL of the markers from ALL of the posts. How would I go about doing this assuming all of my posts have a field called ‘event_map’ to draw location data from? I am stumped and any help appreciated.

  • you need to do something like this:
    the key is = to loop first through all post, save marker/values into an array (without echo something), output one single map, and output the markers from saved array

    <?php 
    $the_query_map = new WP_Query( array( 'post_type' => 'event', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'menu_order' ) );
    
    if($the_query_map->have_posts()) :
    while($the_query_map->have_posts()):
    $the_query_map->the_post();
    $the_ID = get_the_ID();
    $get_google_map = get_field('event_map', $value);
    
    $output_map[$the_ID]['map'] = '<div class="marker" data-lat="'.$get_google_map['lat'].'" data-lng="'.$get_google_map['lng'].'"></div>';
    
    endwhile; endif;
    wp_reset_postdata();
    
    ?><div class="acf-map"><?php
    foreach( $output_map as $key => $map_marker ):
    	echo $map_marker['map'];
    	endforeach;
    	?>
    </div>

    you need to adapt my code that it fit your needs, but i hope with that help you can do it at your own.

  • @mediawerk thanks for your input. The code you provided was working fine. It suddenly stopped working. Any idea what could go wrong?

    Thanks

  • HI @futaba

    It is possible that there has been some change on your site.

    Have you done any plugin/theme update?

    Kindly try deactivating all the plugins and revert to the default wordpress theme and let see if the code still works. It could be a plugin conflict.

    Hope this helps.

  • Its working.

    $get_google_map = get_field('event_map', $value);

    this was edited by client by mistake.

    Thanks 🙂

  • I also noticed that this stopped working completely. Unlike Futaba, it still does not seem to be working for me. Was great for months then one day out of nowhere, failed.
    No idea why to this day so we had to disable the feature completely unfortunately. I always assumed it was something Google changed on their end as our site and code didn’t change at all.

  • Hi @futaba

    I am glad you were able to make it work.

    Feel free to contact us in case something else comes up.

  • Hi @lowercase
    Quick question for you. How did you manage to get your orignal code to work with out referencing the post ID the only way I can get mine to display a single post is to included the post id
    Im using ACF & CPT UI plugins
    Cheers for the advice in advance 🙂

    <?php 
    
    $location = get_field('location_now',22);
    
    if( !empty($location) ):
    ?>
    <div class="acf-map">
        <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
    </div>
    <?php endif; ?>
    

    My aim is to create a stand alone map that loops all the map points but I cant seem to get the template file to work

  • I’m getting a “Notice: Undefined variable: value” using this script. Any ideas how to solve this?

    Note: If I remove “, $value”, it outputs the marker divs, but the map doesn’t appear.

  • @mediawerk Thanks so much , code is working fine!

    Just one question:

    Is it possible to add an URL to each marker?

  • With above code I also ran into php erros that I could not fix.

    I found this, which works nicely and is more flexible, with some controls and shortcode support.

    Hope it helps!

  • I’m getting latlng is not defined error and nothing showing…

Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘Google maps with multiple markers’ is closed to new replies.