Tell us what you are trying to do?
I have a map view that shows the results from a search in another view that you helped me to set up (this was the related support call: https://toolset.com/forums/topic/display-all-directory-entries-on-map-but-list-with-pagination/).
The client has recently noticed that the results display on the map do not respect the distance filter that has been set in the original search.
I can see why this is happening because the radius looks to be hard-coded in the map view (see screenshot).
Is there a way that I can account for the radius that has been selected by the user so that the same results are shown on the map as in the text search results?
For example, if you do a search where the only parameters set are 'Show results within 5 miles of Sheffield, UK', you get 5 therapists listed but many more shown on the map because it is using 40 miles instead of 5 miles.
hidden link
What is the link to your site?
hidden link
Hi,
Welcome to Toolset support. I think I understand the issue. You must override the Map View’s distance settings from the URL params (radius/unit/center), because the Map View keeps its own "map distance filter" settings.
Edit the previously suggested code and update it with this code:
add_filter( 'wpv_view_settings', 'ts_sync_map_distance_from_url', 5, 2 );
function ts_sync_map_distance_from_url( $view_settings, $view_id ) {
// CHANGE THIS to your MAP VIEW ID
if ( (int) $view_id !== 2056 ) {
return $view_settings;
}
if ( isset( $_REQUEST['toolset_maps_distance_radius'] ) && $_REQUEST['toolset_maps_distance_radius'] !== '' ) {
$view_settings['map_distance_filter']['map_distance'] = sanitize_text_field( $_REQUEST['toolset_maps_distance_radius'] );
}
if ( isset( $_REQUEST['toolset_maps_distance_unit'] ) && $_REQUEST['toolset_maps_distance_unit'] !== '' ) {
$view_settings['map_distance_filter']['map_unit'] = sanitize_text_field( $_REQUEST['toolset_maps_distance_unit'] );
}
if ( isset( $_REQUEST['toolset_maps_distance_center'] ) && $_REQUEST['toolset_maps_distance_center'] !== '' ) {
$view_settings['map_distance_filter']['map_center'] = sanitize_text_field( $_REQUEST['toolset_maps_distance_center'] );
}
return $view_settings;
}
This is the same mechanism my colleague used previously (hooking wpv_view_settings and updating map_distance_filter).
Important point: Your search View must use a full page refresh on submit, so the distance params are in the URL for the Map View to read.
We will not be able to provide the Ajax-enabled version as it will be way outside of our support scope, and it will involve complex JavaScript overrides that need a developer to handle.
After the code update, the map will respect:
toolset_maps_distance_radius
toolset_maps_distance_unit
toolset_maps_distance_center
One last point, make sure your view ID that renders the map is "2056". If not, please add the correct ID in the code.
Thanks.