Google使用wordpress类别

时间:2015-11-02 14:51:54

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

我必须使用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;

1 个答案:

答案 0 :(得分:1)

您可以更改标记,将类别slugs添加为类名:

<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');
});

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