简体   繁体   English

使用wordpress类别的Google地图位置过滤器

[英]Google maps location filter using wordpress categories

I have to create a google maps location filter using wordpress categories. 我必须使用wordpress类别创建一个Google地图位置过滤器。

I have created custom post type named "custom" and inside there are several locations created. 我创建了名为“ custom”的自定义帖子类型,并且在其中创建了多个位置。 Each one has got a category and these categories are outputted to the page. 每个人都有一个类别,这些类别将输出到页面。

I'm using ACF Pro google maps extention to output all locations on my map below categories. 我正在使用ACF Pro谷歌地图扩展来输出我的地图上类别以下的所有位置。

Now, i need to create a system that allows me to filter based on category. 现在,我需要创建一个系统,使我可以根据类别进行过滤。 If i click on a category all other locations are hidden. 如果我单击类别,则所有其他位置均被隐藏。

I'm not very strong with javascript but can i use simple add/remove class to show/hide locations on my map? 我对JavaScript的了解不是很强,但是我可以使用简单的添加/删除类在地图上显示/隐藏位置吗?

My current code to output markers on my map: 我目前在地图上输出标记的代码:

   <?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>

And my ACF PRO js code is here: 我的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;

You could change the marker to add the categories slugs as class names like this: 您可以更改标记以将类别标签添加为类名称,如下所示:

<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']; ?>">

Then in your JS hide / show markers dependings of those classes. 然后在JS中隐藏/显示这些类的标记。

For example, add this listener to the add_marker function: 例如,将此侦听器添加到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');
});

It will hide or show the marker depending of a checkbox. 它将根据复选框隐藏或显示标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM