【问题标题】:Google maps location filter using wordpress categories使用 wordpress 类别的谷歌地图位置过滤器
【发布时间】:2016-02-02 12:00:02
【问题描述】:

我必须使用 wordpress 类别创建一个谷歌地图位置过滤器。

我创建了名为“custom”的自定义帖子类型,其中创建了几个位置。每个都有一个类别,这些类别输出到页面。

我正在使用 ACF Pro 谷歌地图扩展来输出我地图上以下类别的所有位置。

现在,我需要创建一个允许我根据类别进行过滤的系统。如果我点击一个类别,所有其他位置都将被隐藏。

我对 javascript 不是很擅长,但我可以使用简单的添加/删除类在我的地图上显示/隐藏位置吗?

我当前在地图上输出标记的代码:

   <?php

$post_type = 'customcat';    

$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );

foreach( $taxonomies as $taxonomy ) :

    $terms = get_terms( $taxonomy, 'orderby=name&hide_empty=1&order=DESC' );

    foreach( $terms as $term ) : ?>

        <div class="town clearfix">
          <h2 class="mid-heading"><?php echo $term->name; ?></h2>          
          <?php
          $args = array(
                  'post_type' => $post_type,
                  'posts_per_page' => -1,  //show all posts
                  'tax_query' => array(
                      array(
                          'taxonomy' => $taxonomy,
                          'field' => 'slug',
                          'terms' => $term->slug,
                      )
                  )

              );
          $posts = new WP_Query($args);

          if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>


        <?php endwhile; endif; ?>
        <?php wp_reset_postdata(); ?>
        </div>
    <?php endforeach;

endforeach; ?>
      </div>  
        <div class="rendered_map">
          <?php 
              query_posts(array( 
                  'post_type' => 'customcat',
                  'posts_per_page' => -1,
              ) );  
          ?>
          <div class="acf-map">
          <?php while (have_posts()) : the_post(); ?>
                  <h2><?php the_title(); ?></h2>
            <?php 
            $location = get_field('aadress');
            if( !empty($location) ):
            ?>
            <div class="marker" style="display:none;" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
              <h4><?php the_sub_field('title'); ?></h4>
              <p><?php echo get_the_title(); ?></p>                            
            </div>
            <?php endif; ?>
          <?php endwhile;?>
          <?php wp_reset_postdata(); ?>
          </div>
        </div>

我的 ACF PRO js 代码在这里:

function new_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               
    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
    });

    // 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 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;

【问题讨论】:

    标签: javascript php wordpress google-maps google-maps-api-3


    【解决方案1】:

    您可以更改标记以将类别 slug 添加为类名,如下所示:

    <div class="marker <?php foreach(get_the_category() as $category) { echo $category->slug . ' ';} ?>" style="display:none;" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
    

    然后在你的 JS 中隐藏/显示这些类的标记。

    例如,将此监听器添加到add_marker 函数中:

    google.maps.event.addListener(marker, 'class_changed', function () {
        if($marker.hasClass($('input[name="marker"]:checked').val())) {
            marker.setVisible(true);
        } else {
            marker.setVisible(false);
        }
    });
    $('input[name="marker"]').change(function() {
        google.maps.event.trigger(marker, 'class_changed');
    });
    

    它将根据复选框隐藏或显示标记。

    【讨论】:

    • 感谢您的回答,但我需要隐藏标记位置按钮,而不是内容框。例如。如果我点击一个类别,所有其他位置标记/点将被隐藏。
    • 它将类添加到用于构建标记的元素中。因此,您可以使用它来附加将根据此值隐藏/显示标记的事件。我在答案中添加了一个示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多