I am trying to implement google maps by acf and everything works like a charm except one thing. I would like to have main icon different than other and icon will be uploaded by acf. Thanks for any hint.
Here is bunch of my code:
<script type="text/javascript">
var locations = [<?php while( $wp_query->have_posts() ){
$wp_query->the_post();
$location = get_field('carte_google');?>
['<?php the_title(); ?> <br/> <?php the_field('map_description'); ?> <?php the_field('pin'); ?>', <?php echo $location['lat']; ?>, <?php echo $location['lng'];?>, <?php $NUM++ ?>],<?php } ?> ];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: new google.maps.LatLng(45.7830954, 24.0697979),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var image = {
url: 'probably here should be image from acf',
};
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: image,
animation: google.maps.Animation.BOUNCE,
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
Thank You for Your help in advance.
There are 2 possible solutions.
1 – Create categories and set the post with these categories (this is useful to when you has many places in map with the same icon)
2 – Create a custom field to place the icon
I will describe both.
By creating categories you can easily to link many locations with the same icon. I did it once and used the categories images plugin. When in loop, just identify the post category and retrieve image for it, something like:
With the second solution you must to create an Advanced Custom Field for some post/page type, this custom field will be of type: “image”. Set the return to be the image URI, not an object as is the default option. For this example, I suppose that you will create a field named map_icon. In your question I can see you already have a custom field named map_description, for this same ACF configuration just add the image custom field. And to show this image you can do this:
Has a lot of time since I did this, but looking in some references like ACF and the Categories Images plugin these 2 solutions must to work to you.
At least I figure out how to display different icon for one post(Head-quarter – pin) and other for the rest projects. Here is code I used and it work for me. Maybe it will be useful for someone else.
I am ordering here post from the oldest to the newest and then I am adding prefix + image.png to the oldest post.
Thank You for Yours posts
];
I hope it will work for You as well