首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ACF Wordpress - Google地图被切断(调整大小)

ACF Wordpress - Google地图被切断(调整大小)
EN

Stack Overflow用户
提问于 2013-11-14 02:07:36
回答 3查看 1.7K关注 0票数 0

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

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

我知道当您没有将这一行插入到代码中时,问题就会发生,但我不太清楚该将其放在何处:

代码语言:javascript
复制
google.maps.event.trigger(map, 'resize');

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

我所说的一个完整的例子是位于http://www.ourdeadradio.com/并点击"Live“菜单。如您所见,地图被切断,标记没有居中。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-11-26 08:34:42

我也遇到过与google.maps.event.trigger(map, 'resize');一样的问题,因为Google使用jQuery选项卡初始化。

检查此工作代码:

代码语言:javascript
复制
(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);

或选中此参考链接: 谷歌地图初始化不使用选项卡

我还创建了JSFidle:示例

我希望这会对你有所帮助:)

票数 3
EN

Stack Overflow用户

发布于 2014-02-19 19:55:43

我也面临着同样的问题,但是地图在一个隐藏的部分中,通过jQuery在触发器点击中显示。所以,我不需要点击地图本身,我希望地图在显示隐藏的div后立即调整大小。

一开始,我试着添加:

代码语言:javascript
复制
google.maps.event.trigger(map, 'resize');

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

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

代码语言:javascript
复制
$(document).on('mouseenter', 'body', function() {
    google.maps.event.trigger(map, 'resize');
    map.setCenter( bounds.getCenter() );
    map.setZoom( 14 );
});

因此,在单击触发器并显示带有map的隐藏div之后,鼠标一移动,映射就会调整大小。

票数 0
EN

Stack Overflow用户

发布于 2014-10-27 01:17:30

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

代码语言:javascript
复制
$('#modal').one('shown.bs.modal', function (e) {
  $(this).find('.acf-map').each(function(){ render_map( $(this) ); });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19968187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档