Reputation: 168
I'm having issues implementing multiple maps on one page but each map is contained into a separate Wordpress post. I realize this question has been asked numerous times but I am having difficulty implementing the solution into Advanced Custom Fields Plugin in Wordpress.
http://wordpress.org/plugins/advanced-custom-fields/
I understand that the problem occurs when you don't have this line inserted into the code, But I'm not exactly sure where to put it:
google.maps.event.trigger(map, 'resize');
And this is my javascript, HTML, CSS for the Google Maps v3 API in this fiddle: http://jsfiddle.net/28uCz/
A full example of what I'm talking about is located at http://www.ourdeadradio.com/ and clicking on the "Live" menu. As you can see, the maps are cut off and the marker is not centered.
Please help me, I'm struggling here! Thank you ahead of time.
Upvotes: 0
Views: 1780
Reputation: 1632
My approach is a bit different where the call is delayed and prevents re-rendering when a modal is fired. It only makes one call per modal, but still requires a function per modal. It allows for continual upgrading of the plugin's function and is more understandable.
$('#modal').one('shown.bs.modal', function (e) {
$(this).find('.acf-map').each(function(){ render_map( $(this) ); });
});
Upvotes: 0
Reputation: 445
I faced this same issue but the map was in a hidden section that was shown on trigger click via jQuery. So instead of having to click on the map itself, I wanted the map to resize as soon as the hidden div was shown.
At first, I tried adding:
google.maps.event.trigger(map, 'resize');
to the trigger event callback but that didn't work for me.
What worked was adjusting the answer given by @Rajnikanth like so:
$(document).on('mouseenter', 'body', function() {
google.maps.event.trigger(map, 'resize');
map.setCenter( bounds.getCenter() );
map.setZoom( 14 );
});
So after the trigger is clicked and hidden div with map is shown, as soon as the mouse is moved, the map resizes.
Upvotes: 0
Reputation: 2785
I have faced same issue with google.maps.event.trigger(map, 'resize');
for Google Map initialize with jQuery tab.
Check this working code:
(function($) {
/*
* render_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
*/
var map;
function render_map( $el ) {
// var
var $markers = $el.find('.marker');
// vars
var args = {
zoom : 16,
center : new google.maps.LatLng(0, 0),
mapTypeId : google.maps.MapTypeId.ROADMAP
};
// create map
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 );
}
/*
* 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
});
// 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( 16 );
}
else
{
// fit to bounds
map.fitBounds( bounds );
}
$(document).on('click', '.map', function() {
google.maps.event.trigger(map, 'resize');
map.setCenter( bounds.getCenter() );
map.setZoom( 16 );
});
}
/*
* 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
*/
$(document).ready(function(){
$('.acf-map').each(function(){
render_map( $(this) );
});
});
})(jQuery);
or check this refrence link: Google map initialize not working with tab
I have also create JSFidle: Sample
I hope this will help you :)
Upvotes: 3