Home › Forums › Front-end Issues › Display google map markers from custom posts on one map
I know this issue has been addressed before, but I just can’t get the code to work.
I am simply trying to get multiple location markers from multiple posts onto one map.
At the moment, google map displays fine, but each post has it’s own map with its corresponding location. I just need to group the markers into one map.
I have looked at using repeater field, but this doesn’t really address the issue, as it only allows multiple markers from a single post, not multiple markers from multiple posts – if that makes sense.
Anyway, I have tried to use the code from the thread below but I just get a white screen
https://support.advancedcustomfields.com/forums/topic/google-maps-with-multiple-markers/#post-52506
Here is my code – help very much appreciated 🙂 Thanks!
<?php
$profile_id = um_profile_id();
$args = array(
'author' => $profile_id,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'travel_plans',
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
if ( $query->have_posts()) : ?>
<?php while ( $query->have_posts() ) : $query->the_post();
$the_ID = get_the_ID();
$$location = get_field('location');
$output_map[$the_ID]['map'] = '<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>';
endwhile;
if( !empty($location) ):?>
<div class="acf-map">
<?php
foreach( $output_map as $key => $map_marker ):
echo $map_marker['map'];
endforeach;
?>
</div>
<?php endif; ?>
<?else: ?>
<?endif; ?>
Maybe there’s an error in the code above, but it looks ok to me.
Anyone?
You will need to adapt the following to match your fields etc:
<?php
$args = array(
'post_type' => 'stockist',
'order' => 'ASC',
'order_by' => 'title',
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$the_id = get_the_ID();
$location = get_field( 'company_address' );
$link = get_field( 'company_link' );
$phone = get_field( 'company_phone' );
$country = get_field( 'country_name' );
$logo = get_field( 'company_logo' );
$size = 'productsml';
if ( ! empty( $location ) ) {
?>
<div class="acf-map">
<div class="marker text-center" data-lat="<?php echo esc_attr( $location['lat'] ); ?>" data-lng="<?php echo esc_attr( $location['lng'] ); ?>">
<?php
if ( $logo ) {
echo wp_get_attachment_image( $logo, $size );
}
?>
<h3><?php the_title(); ?></h3>
<?php
if ( $location ) {
$address = '';
foreach ( array( 'street_number', 'street_name', 'city', 'state', 'post_code', 'country' ) as $i => $k ) {
if ( isset( $location[ $k ] ) ) {
$address .= sprintf( '<span class="segment-%s">%s<br /></span>, ', $k, $location[ $k ] );
}
}
$address = trim( $address, ', ' );
?>
<p><?php echo esc_html( $location['address'] ); ?></p>
<?php
}
?>
<?php if ( $phone ) { ?>
<p><?php echo esc_attr( $phone ); ?> </p>
<?php } ?>
<?php if ( $link ) { ?>
<a class="button" href="<?php echo esc_url( $link ); ?>">Visit Website </a>
<?php } ?>
</div>
<?php
}
endwhile;
endif;
?>
The topic ‘Display google map markers from custom posts on one map’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.