CMS Development

KPotts
Membre

orderBy=geo_distance not working

Résolue

I am building a listing of partners near a user. Relevant code:

 

 

 

{# get lat/long from URL #}

{% set user_lat = request.query_dict['lat'] %}
{% set user_lng = request.query_dict['lng'] %}

{# set query #}
{% set params_by_location = "orderBy=geo_distance(location,"~user_lat~","~user_lng~",mi)__lt=100&" %}


{% for row in hubdb_table_rows(id, params_by_location) %}

{# render name and distance #}
{{ row.hs_name }} || {{ row.location | geo_distance(user_lat, user_lng, "mi") | round(1) }} miles <br>
{% endfor %}

 

 

 

Currently. it pulls from the DB but the results are NOT in order. If I hard-code lat/long values into the query, then it works correctly. It only breaks when I am concantenating variables.

0 Votes
1 Solution acceptée
Indra
Solution
Guide | Partenaire solutions Elite
Guide | Partenaire solutions Elite

orderBy=geo_distance not working

Résolue

Hi @KPotts,

 

Try removing the type of distance and wrapping the params within a if statement to prevent error. This should work:

{% if user_lat && user_lng %}
{% set params_by_location = "orderBy=geo_distance(location,"~user_lat~","~user_lng~")" %}
{% endif %}

 

So your code would look like this:
(I also added a fallback if no query isset.)

{% set user_lat = request.query_dict['lat'] %}
{% set user_lng = request.query_dict['lng'] %}

{# set query #}
{% set params_by_location = '' %}

{# Check if lat and lng exists #}
{% if user_lat && user_lng %}
{% set params_by_location = params_by_location ~ "orderBy=geo_distance(location,"~user_lat~","~user_lng~")" %}
{% endif %}


{% for row in hubdb_table_rows(id, params_by_location) %}

{# render name and distance #}
{{ row.hs_name }} || {{ row.location | geo_distance(user_lat, user_lng, "mi") | round(1) }} miles
<hr>

{% endfor %}

 


Vet Digital - The Growth Agency | HubSpot Solutions Partner Agency

Did my post solve your question? Help the community by marking it as a solution

Voir la solution dans l'envoi d'origine

4 Réponses
KPotts
Membre

orderBy=geo_distance not working

Résolue

Wow. This was insanely helpful. Thank you. It worked. I don't know WHY it worked, but it did. Adding an if statement to avoid errors is a very useful trick.

 

Also, for anyone reading this in the future, I got around the lack of distance filter in the HubDB query by testing the distance in the actual for loop. Code ...

 

{% for row in hubdb_table_rows(id, params_by_location) %}
  {% set distance_from_user = row.location | geo_distance(user_lat, user_lng, "mi") | round(1) %}
  {% set arbitrary_distance = 500 %}
  {% if distance_from_user < arbitrary_distance %}
    {# only show locations within 500 miles #}
    {{ row.hs_name }} || {{ distance_from_user }} miles <br> 
  {% endif %}
{% endfor %}

 

Thank you @Indra very much.

KPotts
Membre

orderBy=geo_distance not working

Résolue

My query specifically uses "lng". I have no problem rendering the query string values. Pulling in the data is not the issue -- it's the function or the query that's not working. This is the problem:

 

{% set params_by_location = "orderBy=geo_distance(location,"~user_lat~","~user_lng~",mi)__lt=100&" %}

I just have no idea what the problem is.

 

0 Votes
Indra
Solution
Guide | Partenaire solutions Elite
Guide | Partenaire solutions Elite

orderBy=geo_distance not working

Résolue

Hi @KPotts,

 

Try removing the type of distance and wrapping the params within a if statement to prevent error. This should work:

{% if user_lat && user_lng %}
{% set params_by_location = "orderBy=geo_distance(location,"~user_lat~","~user_lng~")" %}
{% endif %}

 

So your code would look like this:
(I also added a fallback if no query isset.)

{% set user_lat = request.query_dict['lat'] %}
{% set user_lng = request.query_dict['lng'] %}

{# set query #}
{% set params_by_location = '' %}

{# Check if lat and lng exists #}
{% if user_lat && user_lng %}
{% set params_by_location = params_by_location ~ "orderBy=geo_distance(location,"~user_lat~","~user_lng~")" %}
{% endif %}


{% for row in hubdb_table_rows(id, params_by_location) %}

{# render name and distance #}
{{ row.hs_name }} || {{ row.location | geo_distance(user_lat, user_lng, "mi") | round(1) }} miles
<hr>

{% endfor %}

 


Vet Digital - The Growth Agency | HubSpot Solutions Partner Agency

Did my post solve your question? Help the community by marking it as a solution
Indra
Guide | Partenaire solutions Elite
Guide | Partenaire solutions Elite

orderBy=geo_distance not working

Résolue

Hi @KPotts,

 

You are using 

request.query_dict['lng']

 

Perhaps it will be solved when you change it to:

request.query_dict['lon']

 

If it's still not working, I recommend to check the documentation on how to setup a page with a map. There is the function 'geo_distance' also used. 


Vet Digital - The Growth Agency | HubSpot Solutions Partner Agency

Did my post solve your question? Help the community by marking it as a solution
0 Votes