impress-org / google-maps-builder

One Google Maps plugin to rule them all. Google Maps Builder is intuitive, sleek, powerful and easy to use. Forget the rest, use the best.
https://wordpress.org/plugins/google-maps-builder/
32 stars 9 forks source link

Restrict Markers in Mashup by Term (if on a taxonomy-term page) #111

Open JustinSainton opened 9 years ago

JustinSainton commented 9 years ago

I've got a mashup using Schools as a custom post type, and they're sorted under States as a taxonomy.

I'd expect to be able to set a single map with all the terms, and have a way to embed that map so that when I'm at /schools/oregon, for example, it shows only the markers in the "oregon" taxonomy.

kevinwhoffman commented 9 years ago

I was thinking about this too as I have a Hotels CPT with Regions as a taxonomy.

The thing to keep in mind is that these scenarios would also require adjusting the lat/lng and zoom level of the map. I don't think you want to show an entire map of the US and only have markers visible in Oregon.

I was thinking possibly shortcode attributes to override the map defaults?

[google_maps id="1" lat="123" lng="123" zoom="10" mashup_1="post_type=school&taxonomy=state&terms=oregon"]

Maybe a filter is more appropriate?

JustinSainton commented 9 years ago

@kevinwhoffman FWIW, I found the gmb_localized_data flexible enough to do everything we're talking about here. I can imagine that non-developers would appreciate a UI for that, though.

kevinwhoffman commented 9 years ago

@JustinSainton Thanks for the heads up. Looks like gmb_localized_data is exactly what I need. It does seem like a fairly complex concept for the end user to grasp even with a shortcode available. Feels like developer territory to me.

mathetos commented 9 years ago

@JustinSainton I just want to clarify your issue. Basically, as-is you'd have to create 50 maps, each linked to the State taxonomy. You're saying it's much more elegant to create ONE map with all those taxonomies and then narrow the markers by taxonomy with a shortcode attribute.

Correct?

JustinSainton commented 9 years ago

@mathetos Correct. If it's helpful (and I hope it's not giving away anything from the pro version...if it is...feel free to remove) - here's the solution I came up with:

add_filter( 'gmb_localized_data', function( $data ) {

    if ( ! is_tax( 'state' ) ) {
        return $data;
    }

    $posts = get_posts( array( 'post_type' => 'school', 'state' => get_queried_object()->slug, 'posts_per_page' => 1 ) );
    $post  = $posts[0];

    foreach ( $data as $map_id => $_data ) {
        $data[ $map_id ]['mashup_markers'][0]['terms'] = array( get_queried_object()->term_id );
        $data[ $map_id ]['map_params']['latitude']     = $post->_gmb_lat;
        $data[ $map_id ]['map_params']['longitude']    = $post->_gmb_lng;
    }

    return $data;

} );