GeoPoint

This class represent a point on earth in geographical coordinates: latitude and longitude.

  • Latitude ranges between -90 and 90 degrees, inclusive. Values above or below this range will throw an IllegalArgumentException.

  • Longitude ranges between -180 and 180 degrees, inclusive. Values above or below this range will throw an IllegalArgumentException.

This class cannot be persisted - i.e you can't declare a Realm property that is of type GeoPoint. It is only used as a building block for other geospatial shapes such as GeoBox, GeoPolygon and GeoCircle.

Storing geo points in a model class is currently done using duck-typing, which means that any model class with a specific "shape" can be queried as though it contained a geographical location.

The following is required:

  • A String property with the value of "Point", i.e var type: String = "Point"

  • A List containing a Longitude/Latitude pair: var coordinates: RealmList<Double> = realmListOf()

The recommended approach is encapsulating this inside its own EmbeddedRealmObject, like this

public class Location: EmbeddedRealmObject {
public constructor() // Empty constructor required by Realm. Should not be used.
public constructor(latitude: Double, longitude: Double) {
coordinates.apply {
add(longitude)
add(latitude)
}
}

// Name and type required by Realm
private var coordinates: RealmList<Double> = realmListOf()

// Name and type required by Realm
private var type: String = "Point"

@Ignore
public var latitude: Double
get() = coordinates[1]
set(value) {
coordinates[1] = value
}

@Ignore
public var longitude: Double
get() = coordinates[0]
set(value) {
coordinates[0] = value
}
}

This can then be used like this:

class Restaurant: RealmObject {
var name: String = ""
var location: Location? = null
}

realm.write {
copyToRealm(Restaurant().apply {
name = "McBurger"
location = Location(latitude = 40.730625, longitude = -73.93609)
}
}

val newYork = GeoPoint.create(latitude = 40.730610, longitude = -73.935242)
val searchArea = GeoCircle.create(center = newYork, radius = Distance.fromMiles(2.0))
val restaurants = realm.query<Restaurant>("location GEOWITHIN $0", searchArea).find()

A proper persistable GeoPoint class will be implemented in an upcoming release.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
abstract val latitude: Double

Latitude in degrees.

Link copied to clipboard
abstract val longitude: Double

Longitude in degrees.