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.
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 %}
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&" %}