Geospatial/geolocation for KMM Realm Mongodb

I have a kotlin multiplatform mobile app in android and swiftui, what is the best approach to implement a geospatial/geolocation using realm?

This is kind of an open ended question; a “best approach” will depend on your use case. Realm itself has no knowledge of locations and no geospatial capability - it stores data and can notify your app of changes to that data, and that’s about it.

If you want to implement some type of geofencing or location capability, you would rely on the devices API to provide your app that data, and then store the results in Realm.

At a high level, for example, your devices API can tell you where the device is by asking it’s Location services for a location. In Swift it may be the CLLocationManager to which you could then get coordinates

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
   if let loc: CLLocationCoordinate2D = manager.location?.coordinate {
       print("you are here: \(loc.latitude)    \(loc.longitude)")
   }
}

Beyond that, it would be up to you to implement what is done what those coordinates and the UI.

hey there, Product for Realm & Device Sync here. I am interested in building this feature - I’d love to pick your bring on your use case if you’re keen. If you could drop me a line at ian.ward@mongodb.com and we can set up a time to chat.

Hello, thank you for the reply!

I would like to know if Realm has already implemented that. Like there is in Mongo db: https://www.mongodb.com/docs/manual/geospatial-queries/.

What I need is just to find out if a user point (latlong) is inside a polygon.

Thank you for the info and time!

@Daniel_Gabor

(Realm-ers feel free to correct me)

At this time Realm SDK’s do not currently directly support GeoJSON types for geospatial queries - here’s what is supported: android property types or swift property types

That being said as one option you can directly interface with Atlas via the Realm Swift SDK’s MongoClient with the Query API

Additionally, I believe server functions can also be called which would also provide access to that functionality.

You can also craft your own - we did a little test project a while back using Swifts CLLocationCoordinate2D and Type Projection to work with geospatial data. There’s some example code at that link as well.

You have @Ian_Ward attention in this topic so run with that - having Geospatial ‘stuff’ in the Realm SDK would be (IMO) an important and exciting feature to add!

Hello Jay, I want to add as property to the realm object.
Wouldn’t this work? :

It looks promising but I can’t comment on third party libraries (especially ones I have no experience with).

We don’t have this natively yet but we are interested in building it - you can see a workaround another user leveraged here -