0

我在一个页面上实现多个地图时遇到问题,但每个地图都包含在一个单独的 Wordpress 帖子中。我意识到这个问题已经被问过很多次了,但是我很难将解决方案实施到 Wordpress 中的高级自定义字段插件中。

http://wordpress.org/plugins/advanced-custom-fields/

我知道当您没有将此行插入代码时会出现问题,但我不确定将其放在哪里:

google.maps.event.trigger(map, 'resize');

这是我在这个小提琴中用于 Google Maps v3 API 的 javascript、HTML、CSS:http: //jsfiddle.net/28uCz/

我正在谈论的完整示例位于http://www.ourdeadradio.com/并单击“实时”菜单。如您所见,地图被切断,标记未居中。

请帮助我,我在这里挣扎!提前谢谢你。

4

3 回答 3

3

我在使用 jQuery 选项卡初始化 Google Map 时遇到了同样的问题google.maps.event.trigger(map, 'resize');

检查此工作代码:

(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);

或查看此参考链接: Google map initialize not working with tab

我还创建了 JSFidle:示例

我希望这能帮到您 :)

于 2013-11-26T08:34:42.077 回答
0

我遇到了同样的问题,但地图位于通过 jQuery 触发单击时显示的隐藏部分。因此,不必单击地图本身,我希望地图在显示隐藏的 div 后立即调整大小。

起初,我尝试添加:

google.maps.event.trigger(map, 'resize');

触发事件回调,但这对我不起作用。

有效的是调整@Rajnikanth 给出的答案,如下所示:

$(document).on('mouseenter', 'body', function() {
    google.maps.event.trigger(map, 'resize');
    map.setCenter( bounds.getCenter() );
    map.setZoom( 14 );
});

因此,在单击触发器并显示带有地图的隐藏 div 后,只要移动鼠标,地图就会调整大小。

于 2014-02-19T19:55:43.310 回答
0

我的方法有点不同,其中调用被延迟并在触发模式时防止重新渲染。每个模态只调用一次,但每个模态仍然需要一个函数。它允许插件功能的不断升级,更容易理解。

$('#modal').one('shown.bs.modal', function (e) {
  $(this).find('.acf-map').each(function(){ render_map( $(this) ); });
});
于 2014-10-27T01:17:30.183 回答