Home › Forums › General Issues › Google Maps – Multiple Custom Markers
Hello,
I’ve found lots of code for how to add custom markers to Google Maps, but hardly anything about using different markers on the same map. For instance, I may have 4 markers from a repeater field that I want to appear with a green marker, and the rest of the markers related to a custom post type to appear with a gray marker.
I’ve tried a few different methods, like duplicating the marker variable and changing the name and colors, but everything I try breaks some part of the map.
<style type="text/css">
.acf-map {
width: 100%;
height: 730px;
}
/* fixes potential theme css conflict */
.acf-map img {
max-width: inherit !important;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript">
(function($) {
/*
* new_map
*
* This function will render a Google Map onto the selected jQuery element
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param $el (jQuery element)
* @return n/a
*/
function new_map( $el ) {
// var
var $markers = $el.find('.marker');
// vars
var args = {
zoom : 5,
scrollwheel: false,
center: {lat: 39.8, lng: 98.5795},
mapTypeId : google.maps.MapTypeId.ROADMAP,
styles: [
{
"featureType": "all",
"elementType": "all"
};
// create map
var map = new google.maps.Map( $el[0], args);
// add a markers reference
map.markers = [];
// add markers
$markers.each(function(){
add_marker( $(this), map );
});
// center map
center_map( map );
// return
return map;
}
/*
* add_marker
*
* This function will add a marker to the selected Google Map
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param $marker (jQuery element)
* @param map (Google Map object)
* @return n/a
*/
function add_marker( $marker, map ) {
// var
var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
// // create marker
// var marker = new google.maps.Marker({
// position : latlng,
// map : map
// });
var grayCircle = {
//path: 'M -1,0 A 1,1 0 0 0 1,0 1,1 0 0 0 -1,0 z',
path: google.maps.SymbolPath.CIRCLE,
fillColor: '#888888',
fillOpacity: 1,
scale: 3,
strokeColor: "#888888",
strokeWeight: 3
};
var marker = new google.maps.Marker({
position: latlng,
icon: grayCircle,
map: map
});
// add to array
map.markers.push( marker );
// if marker contains HTML, add it to an infoWindow
if( $marker.html() )
{
// create info window
var infowindow = new google.maps.InfoWindow({
content : $marker.html()
});
// show info window when marker is clicked
google.maps.event.addListener(marker, 'click', function() {
infowindow.open( map, marker );
});
}
}
/*
* center_map
*
* This function will center the map, showing all markers attached to this map
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param map (Google Map object)
* @return n/a
*/
function center_map( map ) {
// vars
var bounds = new google.maps.LatLngBounds();
// loop through all markers and create bounds
$.each( map.markers, function( i, marker ){
var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
bounds.extend( latlng );
});
// only 1 marker?
if( map.markers.length == 1 )
{
// set center of map
map.setCenter( bounds.getCenter() );
map.setZoom( 5 );
}
else
{
// fit to bounds
map.fitBounds( bounds );
}
}
/*
* document ready
*
* This function will render each map when the document is ready (page has loaded)
*
* @type function
* @date 8/11/2013
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
// global var
var map = null;
$(document).ready(function(){
$('.acf-map').each(function(){
// create map
map = new_map( $(this) );
});
});
})(jQuery);
</script>
<div class="portfolio-map">
<div class="acf-map">
<?php
// check if the repeater field has rows of data
if( have_rows('site_details') ):
// loop through the rows of data
while ( have_rows('site_details') ) : the_row();
// display a sub field value
$siteName = get_sub_field('site_name');
$siteLink = get_sub_field('site_link');
$siteAddress = get_sub_field('site_address');
?>
<!-- I would like this set of information to have a green marker --> <div class="marker" data-lat="<?php echo $siteAddress['lat']; ?>" data-lng="<?php echo $siteAddress['lng']; ?>">
<h4><?php echo $siteName; ?></h4>
</div>
<?php
endwhile;
else :
// no rows found
endif;
?>
<?
$args = array(
'post_type' => 'organization',
'posts_per_page' => 99
);
$locationMap = new WP_Query($args);
$count = 0;
?>
<? if($locationMap->have_posts()) : while($locationMap->have_posts()) : $locationMap->the_post();
$count++;
$location = get_field('organization_address');
$siteLocation = get_field('site_address');
?>
<!-- I would like this set of information to have a gray marker --> <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<a href="<?php the_permalink(); ?>">
<img class="popup" src="<?php the_field('logo'); ?>"/>
</a>
<div class="popup-details">
<?php the_field('map_popup_details'); ?>
</div>
<div class="popup-link">
<a href="<?php the_permalink(); ?>">Learn More </a>
</div>
</div>
<? endwhile; endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
If you want to customize the look of the map, please ask the Google Map community about that.
Thanks 🙂
I did. Someone in the Google Maps community told me to try this forum. I guess I’m on my own! I’ll try Stack Overflow. Thanks.
That’s weird. I believe you can get the latitude and longitude from ACF correctly, right? The problem is you want to set a different color for certain markers. If that’s right, I believe you need to use Google maps functions, not ACF functions. So, asking it to the Google Map community should be more appropriate.
I hope this makes sense.
You must be logged in to reply to this topic.
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.