Pan Mluvčí
Pan Mluvčí

Reputation: 1261

SWIFT Realm.io filter by distance

I am trying to achieve filter on realm obj that contains LAT and LNG. Filter will take my current position and SORT datasource to present closest obj to least ones. I tried to solve this with GeoQueries! pod, but it keeps returning err:

'Expected object of type string for property 'lat' on object of type 'E21', but received: 53.48708'

my code:

var datasourceE21Distance:Results<E21>!

func loadDistanceDb() {
    do{
        let tempLat = Double(LocationManager.sharedInstance.latitude)
        let tampLng = Double(LocationManager.sharedInstance.longtitude)


        let realm = try Realm()
        datasourceE21Distance = realm.findInRegion(E21.self, region: mapView.region)

        print(datasourceE21Distance)

    } catch let error as NSError {
        print(error)
    }
}

Upvotes: 0

Views: 346

Answers (1)

marius
marius

Reputation: 7806

GeoQueries expect that the properties for the geocoordinate components are named lat and lng by default and are of type Double or Float.

From the error message, it seems though like your properties would be of type String. These can't be queried by Realm using numeric comparisons.

If you use other names for your properties, then you have to specify these by optional arguments passed to the methods defined by the library.

realm.findInRegion(E21.self, region: mapView.region
       latitudeKey:  "latitude",
       longitudeKey: "longitude"
     )

Upvotes: 1

Related Questions