Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
ammarahw
Contributor III
Contributor III

Creating a filter for distance from a postal code using latitude and longitude fields, a postal code filter and a slider for distance

Hi, I am trying to create a slider for distance that would go from 1 to 100 miles. To set this up I have two variables, a filter, a measure for distance and a dimension to create the filter.

latitude variable:

=First({<postal_code = {"$(=GetFieldSelections(postal_code))"}>} lat)

longitude variable:

=First({<postal_code = {"$(=GetFieldSelections(postal_code))"}>} lon)

A filter called Postal Code that is just a drop down that lets you choose a postal code from the field postal_code.

The measure for distance is called Distance Between Zips and I wrote it like this:

sqrt(
(
(69.1 * (lat - (v_selected_lat))) * (69.1 * (lat - (v_selected_lat)))
) +
(
(69.1 * cos((v_selected_lat)) * (lon - (v_selected_lon))) * (69.1 * cos((v_selected_lat)) * (lon - (v_selected_lon)))
)
)

After that I created a dimension called Distance Filter which is this:

=if([Distance Between Zips] <= 5 , '5 Miles', 
if([Distance Between Zips] <= 10 , '10 Miles',
    if([Distance Between Zips] <=15 , '15 Miles',
        if([Distance Between Zips] <=20, '20 Miles', 
            if([Distance Between Zips] = 25, '25 Miles',
                if ([Distance Between Zips] <= 50, '50 Miles')
                  )
                 )
                )
               )
              )

 

At first I put the Distance Field into a drop down to see if it would work when I chose a zip code from the postal_code filter but it didn't work. The filter actually needs to be a slider.

 

Is there a better way to do what I am trying to do? I have been trying to piece together a process off of previous tickets like these:

https://community.qlik.com/t5/New-to-Qlik-Analytics/50-km-radius-in-qliksense-maps/td-p/1735868

https://community.qlik.com/t5/App-Development/Selecting-field-value-based-on-other-selection-field/t...

https://community.qlik.com/t5/QlikView-App-Dev/Show-distance-between-two-points/td-p/561286

 

Labels (6)
1 Reply
F_B
Specialist II
Specialist II

Hi @ammarahw ,

there’s a more accurate way to calculate the distance between two points on the Earth using the Haversine formula, which is commonly used for this purpose:

=ACOS(
SIN(RADIANS(lat)) * SIN(RADIANS($(v_selected_lat))) +
COS(RADIANS(lat)) * COS(RADIANS($(v_selected_lat))) *
COS(RADIANS(lon) - RADIANS($(v_selected_lon)))
) * 3959

 

This formula calculates the great-circle distance between two points on a sphere given their longitudes and latitudes, and 3959 is the Earth's radius in miles.

Anyway, nice job! Hope this can help you improve your app.