StackSurfer
StackSurfer

Reputation: 79

Google maps location filter using wordpress categories

I have to create a google maps location filter using wordpress categories.

I have created custom post type named "custom" and inside there are several locations created. 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.

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?

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:

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;

Upvotes: 0

Views: 2176

Answers (1)

vard
vard

Reputation: 4156

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.

For example, add this listener to the add_marker function:

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.

Upvotes: 1

Related Questions