Skip Navigation

[Resolved] Limiting view's results based on user location

This support ticket is created 4 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 4 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1758965

Hi,

I am using toolset views. I integrated a google map but in order to limit the number of requests, I want to start the markers with a limited view based on the users geolocation.
How to achieve this ?

#1759093

Hi,

Thank you for waiting.

I've performed some tests on my website and the only way that I could make this work was by using two identical views, with different query filter settings.

In your current/original view you can set the "Custom Search Settings" to "Full page refresh when visitors click on the search button" and then also include a submit button in its "Search and Pagination" section.

Next, please create a duplicate of this view and just update its distance query filter to the static/fixed option "Distance center is set from user location".

When a view's search form is submitted, it appends "wpv_filter_submit" parameter in the URL, which can be used to detect whether the manual search has been performed or not.

You can register a new custom shortcode, that can check that URL parameter's value and then show the correct view, accordingly.

For example:


add_shortcode('show_distance_search_views', 'show_distance_search_views_fn');
function show_distance_search_views_fn() {
	
	ob_start();

	if( (empty($_GET['wpv_filter_submit'])) && (!isset($_GET['wpv_filter_submit'])) ) {
		echo do_shortcode('[wpv-view name="new-view-slug"]');
	} else {
		echo do_shortcode('[wpv-view name="original-view-slug"]');
	}

	return ob_get_clean();
	
}

Note: You'll replace "new-view-slug" and "original-view-slug" with the actual slugs of your new and the existing views, respectively.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

On the page where you're currently showing the original view, you can remove its shortcode and add this new shortcode instead:
[show_distance_search_views]

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1759969

Ok thx Waqar.
I even found a better solutions thanks to your input.

I only show the map when there is a selection.
add_shortcode('variable_map', 'mailbox_show_distance_search_views_fn');
function mailbox_show_distance_search_views_fn() {

ob_start();

if( (empty($_GET['wpv_filter_submit'])) && (!isset($_GET['wpv_filter_submit'])) ) {

} else {
echo do_shortcode('[wpv-map-render map_id="map-7" map_height="400px" cluster="on" marker_icon="//vjf.mailbox-marketing8.be/wp-content/uploads/2020/08/Marker-small.png" background_color="#1e73be"][/wpv-map-render]');
}

return ob_get_clean();

}

One last thing maybe. I would like to do this with ajax. Off course the "$_GET['wpv_filter_submit']" does not work with ajax. Any idea for this ?

#1760427

Thanks for writing back.

If your goal is to just hide the map until the search has been performed, you have these options:

Solution 1:

This will work, only if you're showing map's shortcode anywhere inside the "[wpv-filter-start hide="false"]...[wpv-filter-end]" tags in the "Search and Pagination" section or inside the "[wpv-layout-start]...[wpv-layout-end]" tags in the "Loop Editor" section.

Within these tags, you can get the value from the "$_GET" superglobal, even if the view is set to update results through AJAX.

Note: You can change "$_GET['wpv_filter_submit']" to "$_GET['wpv_view_count']" which would work for AJAX and non-AJAX, both cases.

This means that only within the tags specified above, you can either use the custom shortcode from your reply or a conditional check for the "wpv_view_count" parameter, to show the map.
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-search-term )

Example:


[wpv-conditional if="( '[wpv-search-term param='wpv_view_count']' ne '' )"]
// Add map shortcode here
[/wpv-conditional]

Solution 2:

This solution is for the case when you need the map, outside the tags specified in the solution 1.

a). You'll add your map's shortcode, anywhere on the page but inside a special div container that is hidden when the page initially loads, for example:


<div class="map-container" style="display:none;">
// Add map shortcode here
</div>

b). Next, in the "JS editor", you can add some custom script, that will show this special map container when the search results have been updated through the AJAX:


jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function( event, data ) {
	jQuery('div.map-container').show();
});

I hope this helps.