Skip Navigation

[Resolved] Display view/grid of nearby places relative to the current place

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

Problem:
Display view/grid of nearby places relative to the current place

Solution:

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/display-view-grid-of-nearby-places-relative-to-the-current-place/#post-2054213

Relevant Documentation:

This support ticket is created 3 years, 7 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Alexander Rothschild 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2052891
1890979-Screenshot_2020_12_31_030719.jpg

This has been brought up before, but raising this up again due to a big limitation of the proposed solution.

So as the title says, the goal is to browse from one place to another nearby one. Here's a full working demo - hidden link

The current solution relies on a code snippet in functions.php:

add_filter( 'wpv_view_settings', 'func_filter_distance_center_address', 10, 2 );
function func_filter_distance_center_address( $view_settings, $view_id ) {
    
  global $post;
    
    if ( $post->post_type == 'place' ) { 
      
		$latlong=types_render_field("place-address",array('format'=>"FIELD_LATITUDE,FIELD_LONGITUDE",'item'=>$post->ID));
   
        if(!empty($latlong)) {

              $latlon =  explode(",",$latlong);

              $view_settings['map_distance_filter']['map_center_lat'] = $latlon[0];
              $view_settings['map_distance_filter']['map_center_lng'] = $latlon[1];
        }
    }
    return $view_settings;
}

It is done this way because Toolset does not have a UI to specify a map center using a custom field of the current post.

The issue arises when there are multiple views on a page since $view_settings['map_distance_filter']['map_center_lat'] = $latlon[0]; affects all of them.

Here's the same page with the snippet disabled - hidden link (random places appear in the 'nearby places' view). But here you see that the other views (such as quotes) start showing items.

I would appreciate any additional direction on this. Is there a way to set a map center for each view individually? Would love to see a native UI support for this in ideal scenario.

Or perhaps other views should not even react to $view_settings['map_distance_filter']['map_center_lat'] = $latlon[0] , since their query has nothing to do with distance at all? 🤷‍♀️

If mapcenter could be provided with a shortcode like in the screenshot, that would work as well.

Cheers,
Alex

#2052955

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

It is done this way because Toolset does not have a UI to specify a map center using a custom field of the current post.
==>
Ok, this is known.

The issue arises when there are multiple views on a page since $view_settings['map_distance_filter']['map_center_lat'] = $latlon[0]; affects all of them.
==>
I'm not sure what you mean here. Can you please share it by example.

Here's the same page with the snippet disabled - hidden link (random places appear in the 'nearby places' view). But here you see that the other views (such as quotes) start showing items.
==>
What item you mean shows the item? you wrote "But here you see that the other views (such as quotes) start showing items.", I'm not sure about what filter option you are talking about.

I would appreciate any additional direction on this. Is there a way to set a map center for each view individually? Would love to see a -native UI support for this in ideal scenario.
==>
Well, the code we wrote in such a way that its getting the lat,lon of the currently displayed post from the custom field "place-address": so, we are actually getting the map center from the custom field "place-address" and it will be fetched from the currently displayed post as you can see the following line is added within code I shared before:

  $latlong=types_render_field("place-address",array('format'=>"FIELD_LATITUDE,FIELD_LONGITUDE",'item'=>$post->ID));

Or perhaps other views should not even react to $view_settings['map_distance_filter']['map_center_lat'] = $latlon[0] , since their query has nothing to do with distance at all? 🤷‍♀️
==>
What you mean by other view? do you mean when you filter your view, select the option from the select dropdown "NEARBY", it shows wrong results?

If mapcenter could be provided with a shortcode like in the screenshot, that would work as well.
=>
As you know, its not working as you know from the previous ticket and that will also not work as you want map center to be with specific post.

If you can share what is your expected result with what URL and what steps I have to follow and share admin details I would be happy to look at it again. Please share all required details and answer all above questions and lets see what we can do here.

#2053011
Screenshot 2021-05-12 170601.jpg

Adding a picture to clarify my message. Essentially, the mapcenter snippet fixes view 2, but completely breaks view 1 and view 3.

Expected outcome - all three views should work correctly. Mapcenter should not get applied to view 1 and view 3.

#2053039

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to adjust the following shortcode with view ID.

add_filter( 'wpv_view_settings', 'func_filter_distance_center_address', 10, 2 );
function func_filter_distance_center_address( $view_settings, $view_id ) {
     
  global $post;
    if (  $view_id == 647  ) { 
    if ( $post->post_type == 'place' ) { 
       
        $latlong=types_render_field("place-address",array('format'=>"FIELD_LATITUDE,FIELD_LONGITUDE",'item'=>$post->ID));
    
        if(!empty($latlong)) {
 
              $latlon =  explode(",",$latlong);
 
              $view_settings['map_distance_filter']['map_center_lat'] = $latlon[0];
              $view_settings['map_distance_filter']['map_center_lng'] = $latlon[1];
        }
    }
}
    return $view_settings;
}

As you can see the above code I have bind to your 2nd view whose ID is 647 with if conditoin.

 if (  $view_id == 647  ) { 

now, the above code should run for view ID 647 only. Do you see it working as expected.

#2054213

Here's a final version of the code that does not rely on a hardcoded ID. The solution turned out to be properly detecting the geo-based views with the following line:

!empty($view_settings['map_distance_filter']) 

...and injecting the mapcenter only into the views that passed.

add_filter( 'wpv_view_settings', 'func_filter_distance_center_address', 10, 2 );
function func_filter_distance_center_address( $view_settings, $view_id ) {
     
  global $post;
    
  	//Only post_type 'place' has a place-address that can be injected - skip others
  	//Only geo-filtered views (have a non-empty map_distance_filter) need a map center - skip others
    if ( $post->post_type == 'place' && !empty($view_settings['map_distance_filter']) ) { 
      
		$latlong=types_render_field("place-address",array('format'=>"FIELD_LATITUDE,FIELD_LONGITUDE",'item'=>$post->ID));
   
        if(!empty($latlong)) {

              $latlon =  explode(",",$latlong);

              $view_settings['map_distance_filter']['map_center_lat'] = $latlon[0];
              $view_settings['map_distance_filter']['map_center_lng'] = $latlon[1];
        }
    }
    return $view_settings;
}