Skip to content

Conversation

@MoritzLost
Copy link
Contributor

@MoritzLost MoritzLost commented Jun 11, 2025

Description

Proof-of-concept for distance queries for addresses, as suggested in #17227

The PR adds a AddressQuery::distanceTo() method to calculate the distance of an address to a given place, limit the query by minimum and/or maximum distance, and order results by distance.

Usage:

{# Calculate the distance to the given coordinates, and order the results by distance. #}
{% set addresses = craft.addresses()
    .distanceTo({
        latitude: 50.93515,
        longitude: 6.936852,
    })
    .orderBy('distance ASC')
    .all()
%}

{# Calculate the distance to the given coordinates, and only
 # returns results with a distance between 10 and 50 kilometers.
 #}
{% set addresses = craft.addresses()
    .distanceTo({
        latitude: 50.93515,
        longitude: 6.936852,
        min: 10000,
        max: 50000,
    })
    .all()
%}

Definitely more of a proof of concept, there are a couple of things left to do:

  • The code is only tested with MySQL (requires 8+), will probably need some adjustments for PostgreSQL.
  • Align more closely with Craft coding standards
  • Add better validation and error handling for invalid inputs to the distanceTo() function
  • The distance property will be a float representing the distance in meters, and min and max expect a distance in meters as well. Might want to support both metric and imperial units?
  • A filter to turn the distance property into a localized string (similar to the money or date filters) would be helpful.
  • Is it really necessary to add all conditions to both the query and subQuery? I don't completely understand this, but in my testing, adding the conditions only to the main query resulted in incorrect results, especially with pagination.

Related issues

#17227

@MoritzLost MoritzLost force-pushed the feature/address-distance-to-query branch from b1e97a2 to 2f57c4a Compare June 11, 2025 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant