Skip Navigation

[Resolved] Reload View after map changed_center

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to trigger an AJAX update of my View using custom code that responds to a Map event.

Solution: First, set up the View to update with AJAX whenever an input changes. Then throw a generic 'change' event on any filter input with the class 'js-wpv-filter-trigger':

jQuery(".js-wpv-filter-trigger:first").trigger('change');
This support ticket is created 6 years, 9 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by andyK-6 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#629630

Tell us what you are trying to do?
I try to reload the view with ajax after the map center is changed.

Is there any documentation that you are following?
yes: https://toolset.com/forums/topic/ajax-filtering-with-jquery-call-ajax-filtering-with-jquery/
and others...

What is the link to your site?
hidden link

I have this code, all is working exept the trigger at the end!

jQuery( document ).on('js_event_wpv_addon_maps_init_map_completed', function( event, event_settings ) {
    var i = 0;//first time not
    var map = WPViews.view_addon_maps.get_map('map-1');

	map.addListener('idle', function() {
        if(i){
          	jQuery('#toolset-maps-distance-center').val(map.getCenter().lat() + ',' + map.getCenter().lng());
          	jQuery('#toolset-maps-distance-value').val(getBoundsRadius(map.getBounds()));
		//alert('Center: ' + map.getCenter() + '\nZoom: ' + map.getZoom()+ '\nRadius: ' + getBoundsRadius(map.getBounds()));
          
          	var fil = jQuery("form[name='wpv-filter-336-TCPID93']");
		var view_number = fil.data( 'viewnumber' );
                jQuery('document').trigger('js_event_wpv_parametric_search_triggered', [ { view_unique_id: view_number, form: fil, force_form_update: false, force_results_update: true } ] );
		}
		i=1;
        });
});

function getBoundsRadius(bounds){
  // r = radius of the earth in km
  var r = 6378.8
  // degrees to radians (divide by 57.2958)
  var ne_lat = bounds.getNorthEast().lat() / 57.2958
  var ne_lng = bounds.getNorthEast().lng() / 57.2958
  var c_lat = bounds.getCenter().lat() / 57.2958
  var c_lng = bounds.getCenter().lng() / 57.2958
  // distance = circle radius from center to Northeast corner of bounds
  var r_km = r * Math.acos(
    Math.sin(c_lat) * Math.sin(ne_lat) + 
    Math.cos(c_lat) * Math.cos(ne_lat) * Math.cos(ne_lng - c_lng)
    )
  return r_km
}
#629802

It appears the form is configured so that clicking "Submit" is required to update the form, and the page will reload. So you can simply submit the form at any time to trigger an update:

jQuery('form[name="wpv-filter-336-TCPID93"]').submit();
#630384

This reload's the Page.

I need a solution with ajax!

#630416

First you must edit this View and set it to perform AJAX updates. Look for the Custom Search panel in the View Editor screen. If you cannot find this panel, click "Screen Options" in the top right corner of the editor screen. You can enable the Custom Search panel here. Then scroll down to the Custom Search panel and select the option to update the View with AJAX whenever any input is changed.

Then to trigger an AJAX update programmatically, you can throw a generic 'change' event on any filter element with the class "js-wpv-filter-trigger", something like:

jQuery(".js-wpv-filter-trigger:first").trigger('change');

That should automatically create the correct event payload and trigger the appropriate updates.

#630673

Thanks