Select to view content in your preferred language

Reverse Geocode Max Distance

640
2
Jump to solution
02-21-2024 08:15 AM
smithse
Occasional Contributor

Hi, all,

I am brand new to SwiftUI and in the process of converting an app built in AppBuilder to a native Swift app.

Using the example in the Samples app, I am reverse geocoding a point, and using a custom locator. I would like to include the functionality whereby a user can tap and drag the magnifier around, or tap on a location and reverse geocode that point. So far, I have achieved this. 

What I am trying to solve (if possible) is to reduce the maximum distance at which a user will see a reverse geocode result. Currently, I am getting a geocode result, even when tapping far from the feature.

Using the code examples, and reading the documentation, I see that the range is limited to between 1000 and 50000m. 

Does anyone know if I can possibly override this??

Code is from: 

final public class ReverseGeocodeParameters

 

 

    /// Distance specifying with location the area where candidates will be searched in meters. If `nil`, there is no maximum.
    ///
    /// Distance specifying with location the area where candidates will be searched [meters].
    /// Supported value in a range between 1000 and 50000 meters, 1000 meters by default.
    final public var maxDistance: Double?

 

 

0 Kudos
1 Solution

Accepted Solutions
Destiny_Hochhalter
Esri Contributor

Hi @smithse,

Thanks for your question. `ReverseGeocodeParameters.maxDistance` is limited to 1000 meters, however, the results from the locator task can be filtered by distance.

To filter the results by distance, use the geometry engine to calculate the distance between two points. Here is an example that filters out results greater than 100 meters. Note that to calculate the distance between two points they must have the same spatial reference.

let maxDistance = 100.0

let filteredResults = geocodeResults.filter { result in
    if let displayLocation = result.displayLocation,
       let spatialReference = normalizedPoint.spatialReference,
       let projectedPoint = GeometryEngine.project(displayLocation, into: spatialReference) {
        let distance = GeometryEngine.distance(from: normalizedPoint, to: projectedPoint)
        return distance < maxDistance
    } else {
        return false
    }
}



View solution in original post

2 Replies
Destiny_Hochhalter
Esri Contributor

Hi @smithse,

Thanks for your question. `ReverseGeocodeParameters.maxDistance` is limited to 1000 meters, however, the results from the locator task can be filtered by distance.

To filter the results by distance, use the geometry engine to calculate the distance between two points. Here is an example that filters out results greater than 100 meters. Note that to calculate the distance between two points they must have the same spatial reference.

let maxDistance = 100.0

let filteredResults = geocodeResults.filter { result in
    if let displayLocation = result.displayLocation,
       let spatialReference = normalizedPoint.spatialReference,
       let projectedPoint = GeometryEngine.project(displayLocation, into: spatialReference) {
        let distance = GeometryEngine.distance(from: normalizedPoint, to: projectedPoint)
        return distance < maxDistance
    } else {
        return false
    }
}



smithse
Occasional Contributor

Hi @Destiny_Hochhalter ,

Brilliant, this works great. I fiddled with the extents, thinking there was a way to use that, but instead, this solves it for me! Thanks.

Cheers,
Sean