Skip Navigation

[Waiting for user confirmation] Manually changing user geolocation within view results

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 3 replies, has 1 voice.

Last updated by Minesh 2 days, 15 hours ago.

Assisted by: Minesh.

Author
Posts
#2827280

This is a continuation of sorts from a previous support ticket (https://toolset.com/forums/topic/conditional-display-inside-of-a-for-each-based-on-map-distance/)
I am attempting to compare the individual values of a repeatable Address field against a map search radius. In the previous ticket, we determined that it's not possible to filter content within a for-each. And creating a repeatable field group is not a viable option for my users.

Further experimentation made it clear that there are a few limitations working against me:
- toolset-maps-distance-value can only compare the origin string against a dynamic destination.
- toolset-maps-distance-conditional-display compares a dynamic origin with a destination string.
- there is no built-in way to calculate the distance between an origin string and a destination string. Is there?

With the following view output, I was able to separate the repeatable address field into single values, and then compare that value against the user's location. This works great to eliminate results outside of the radius!
Except that it only works if the user is performing a search within the radius of their own location.
hidden link

So my question is, can I set the value of the user's location detected in 'toolset-maps-distance-conditional-display' to always equal the search filter 'toolset_maps_distance_center'?
For instance, something like [wpv-geolocation location='toolset_maps_distance_center'][/wpv-geolocation]

Thank you for your assistance.

[wpv-layout-start]
[wpv-items-found]

<p>User location: [wpv-geolocation]Yes[/wpv-geolocation]<br>
Origin address: [wpv-search-term param="toolset_maps_distance_center"]<br>
Distance limit: [wpv-search-term param="toolset_maps_distance_radius"]</p>

<!-- wpv-loop-start -->
<wpv-loop>
<div id="item-[wpv-post-id]">
<!-- Declare a result within the search radius. -->
<h4>[wpv-post-body view_template="full-name"][wpv-post-body view_template="workshop-title"]</h4>
<p class="uk-text-meta">[toolset-maps-distance-value origin_source='url_param' postmeta='wpcf-address' unit='mi'] is less than [wpv-search-term param='toolset_maps_distance_radius']</p>

    <!-- Use for-each to separate repeatable address field into individual address values. -->
    [wpv-for-each field="wpcf-address"]
    <!--
    Use distance conditional to compare individual address value against the user's location and radius value.
    This only works if the results from the search are also within the user's radius.
    -->
    [toolset-maps-distance-conditional-display location='[types field="address"][/types]' distance='[wpv-search-term param="toolset_maps_distance_radius"]']
    <!-- individual addresses within user radius are displayed and create a marker -->

  • [types field="address"][/types] [wpv-map-marker map_id='map-1' marker_id='marker-[wpv-post-id][types field="address"][/types]' address='[types field="address"][/types]'][/wpv-map-marker]
  • [/toolset-maps-distance-conditional-display]
    [/wpv-for-each]

</div>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

#2827321

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share admin access details and problem URL and tell me what is your expected result on that page.

As I understand - you want to use the current user's location as distance center?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2827522

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Minesh is not available today. He will be here tomorrow to look into the issue.

I checked the login information and it worked for me. So please wait for minesh tomorrow and he will be with you.

Thanks.

#2828073

Minesh
Supporter

Languages: English (English )

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

There is no such feature to display the distance between two address string.

But here is a sample custom shortcode that may help you and you are allowed to customize it as required.

You cqan add the following custom shortcode to "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

function get_distance_between_two_addresses($atts) {
    $atts = shortcode_atts(
        array(
            'from'  => '',
            'to'    => '',
            'unit'  => 'km', // Default to kilometers
        ),
        $atts,
        'distance'
    );

    /// add your google map API key
    $api_key = 'YOUR_GOOGLE_MAPS_API_KEY';

    // Encode addresses
    $from = urlencode($atts['from']);
    $to = urlencode($atts['to']);

    // Determine unit system
    $unit_system = strtolower($atts['unit']) === 'miles' ? 'imperial' : 'metric';

    // Build API URL with unit parameter
    $url = "<em><u>hidden link</u></em>{$from}&destinations={$to}&units={$unit_system}&key={$api_key}";

    // Use cURL to fetch data
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($ch);

    if (curl_errno($ch)) {
        return 'Request error: ' . curl_error($ch);
    }

    curl_close($ch);

    // Decode JSON
    $data = json_decode($response, true);

    if ($data['status'] !== 'OK') {
        return 'Invalid response from Google API.';
    }

    $element = $data['rows'][0]['elements'][0];

    if ($element['status'] === 'OK') {
        $distance = $element['distance']['text'];
        $duration = $element['duration']['text'];
        return "Distance: {$distance} (Estimated time: {$duration})";
    } else {
        return 'Could not calculate distance.';
    }
}
add_shortcode('show_distance_difference', 'get_distance_between_two_addresses');

- You can add the google map API key

You can call the above shortcode as:

[show_distance_difference from="London, UK" to="Manchester, UK" unit="km"]

[show_distance_difference from="New York, NY" to="Chicago, IL" unit="miles"]