Realm GeoQueries made easy

Overview

RealmGeoQueries

cocoapods carthage issues stars license

RealmGeoQueries simplifies spatial queries with Realm Cocoa. In the absence of and official functions, this library provide the possibility to do proximity search. It's not necessary to include Geohash or other types of indexes in the model class as it only needs latitude and longitude properties.

How To Get Started

Installation with CocoaPods

platform :ios, '9.0'
pod "RealmGeoQueries"

Installation with Carthage

Add to mhergon/RealmGeoQueries project to your Cartfile

github "mhergon/RealmGeoQueries"

Drag GeoQueries.framework, RealmSwift.framework and Realm.framework from Carthage/Build/ to the “Linked Frameworks and Libraries” section of your Xcode project’s “General” settings.

Only on iOS/tvOS/watchOS: On your application targets "Build Phases" settings tab, click the "+" icon and choose "New Run Script Phase". Create a Run Script with the following contents:

/usr/local/bin/carthage copy-frameworks

and add the paths to the frameworks you want to use under "Input Files", e.g.:

$(SRCROOT)/Carthage/Build/iOS/GeoQueries.framework
$(SRCROOT)/Carthage/Build/iOS/Realm.framework
$(SRCROOT)/Carthage/Build/iOS/RealmSwift.framework

Swift Package Manager

Once you have your Swift package set up, adding RealmGeoQueries as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/mhergon/RealmGeoQueries.git", from: "1.4.0")
]

Manually installation

Download (right-click) and add to your project.

Requirements

Version Language Minimum iOS Target
1.4 Swift 5.x / Realm 5.x iOS 9
1.3 Swift 4.x / Realm 3.x iOS 9
1.2 Swift 3.0 / Realm 2.x iOS 9
1.1 Swift 2.x / Realm 2.x iOS 8

Usage

First, import module;

import GeoQueries

Model must have a latitude and longitude keys, that have to be named "lat" and "lng" respectively. You can use another property names (use "latitudeKey" and "longitudeKey" parameters).


Search with MapView MKCoordinateRegion;

let results = try! Realm()
    .findInRegion(type: YourModelClass.self, region: mapView.region)

Search around the center with radius in meters;

let results = try! Realm()
    .findNearby(type: YourModelClass.self, origin: mapView.centerCoordinate, radius: 500, sortAscending: nil)

Filter Realm results with radius in meters;

let results = try! Realm()
    .objects(YourModelClass.self)
    .filter("type", "restaurant")
    .filterGeoRadius(center: mapView.centerCoordinate, radius: 500, sortAscending: nil)

See GeoQueries.swift for more options.

Contact

License

Licensed under Apache License v2.0.
Copyright 2020 Marc Hervera.

Comments
  • Collection Format Different

    Collection Format Different

    Hello,

    I have collection which have following format:

    class Account: Object { @objc dynamic var GeoLocation: A360_M_Account_V2_GeoLocation? }

    class A360_M_Account_V2_GeoLocation: EmbeddedObject { let coordinates = RealmSwift.List() @objc dynamic var type: String? = nil }

    How to sort by distance with these kind of format??

    Thanks in advance.

    opened by vishaldroisys 5
  • error: could not build Objective-C module 'GeoQueries'

    error: could not build Objective-C module 'GeoQueries'

    Hi,

    When I try print out one of my objects to the debugger console area I get the following error:

    (lldb) po events
    error: in auto-import:
    failed to get module 'Pulse' from AST context:
    <module-includes>:2:9: note: in file included from <module-includes>:2:
    #import "Headers/GeoQueries-Swift.h"
            ^
    
    error: /Users/gs/Library/Developer/Xcode/DerivedData/Pulse-cvkoiqauohlejddryiycdlpbhhev/Build/Products/Debug-iphonesimulator/RealmGeoQueries/GeoQueries.framework/Headers/GeoQueries-Swift.h:141:12: error: cannot find interface declaration for 'RealmSwiftObject'
    @interface RealmSwiftObject (SWIFT_EXTENSION(GeoQueries))
               ^
    
    error: could not build Objective-C module 'GeoQueries'
    

    Any idea what could be the issue?

    opened by grantspilsbury 4
  • any chance to have an android compatbile version ?

    any chance to have an android compatbile version ?

    hello,

    you have done here a great work, while the realm team is not looking to solve this issue anytime soon.

    i am wondering if there is a way to implement your librray on android.

    best

    opened by Devmaticz 1
  • Sort does not return  Results<Element>

    Sort does not return Results

    I need to get results from a sortByDistance as a Results is there something I am missing as some of the other calls seem to return this properly instead of an array.

    opened by justinmiller62 1
  • Terminating app due to uncaught exception 'Invalid value', reason: 'Expected object of type string for property 'lat' on object of type 'BusStop', but received: 16.80292'

    Terminating app due to uncaught exception 'Invalid value', reason: 'Expected object of type string for property 'lat' on object of type 'BusStop', but received: 16.80292'

    This is My Realm Model

    `class BusStop: Object{ @objc dynamic var id: Int = 0 @objc dynamic var name_en: String = "" @objc dynamic var name_mm: String = "" @objc dynamic var lat: String = "" @objc dynamic var lng: String = "" @objc dynamic var relations: String = "" @objc dynamic var relation_count: Int = 0

    var coordinate: CLLocationCoordinate2D {
        let coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(exactly: Double(lat)!)!, longitude: CLLocationDegrees(exactly: Double(lng)!)!)
        return coordinate
    }
    

    // override static func primaryKey() -> String?{ // return "id" // } }`

    opened by lawinko 1
  • MKCoordinateRegionMakeWithDistance was obsoleted in Swift 3

    MKCoordinateRegionMakeWithDistance was obsoleted in Swift 3

    Hi! I'm trying to use your library with Xcode 10.1 and Swift 4.2 but I having the following error:

    'MKCoordinateRegionMakeWithDistance' has been replaced by 'MKCoordinateRegion.init(center:latitudinalMeters:longitudinalMeters:)'

    Please add support for Swift 4.2

    Thanks

    opened by dvera128 1
  • Unnormalized longitude fails to compare

    Unnormalized longitude fails to compare

    It's more than an inconvenience than a bug. For longitude ranges from [0, 360) that some data sources use, it fails to determine proximity.

    Maybe something like this before comparison?

    public func wrapLongitude(_ longitude: Double) -> Double {
        let wrapped = fmod(longitude, 360)
        return wrapped < 180 ? wrapped : wrapped - 360
    }
    
    opened by DJBen 1
  • Mapbox Compatibility (MGLMapView)

    Mapbox Compatibility (MGLMapView)

    Hi, I am using RealmGeoQueries with Mapbox, so I added this extension to RealmGeoQueries.swift

    public extension MGLMapView {
        
       
        var geoBox: GeoBox {
            
            let maxLat = self.visibleCoordinateBounds.ne.latitude
            let minLat = self.visibleCoordinateBounds.sw.latitude
            let maxLng = self.visibleCoordinateBounds.ne.longitude
            let minLng = self.visibleCoordinateBounds.sw.longitude
            
            return GeoBox(
                topLeft: CLLocationCoordinate2D(latitude: maxLat, longitude: minLng),
                bottomRight: CLLocationCoordinate2D(latitude: minLat, longitude: maxLng)
            )
            
        }
        
    }
    

    I would have filed a pull request, however I don't know how to make the Import MapBox optional for those who don't want to use it.

    opened by superandrew 1
Releases(v1.4.0)
Owner
Marc Hervera
 iOS, macOS, tvOS, watchOS, Vapor developer, instructor and more... 
Marc Hervera
CoreData/Realm sweet wrapper written in Swift

What is SugarRecord? SugarRecord is a persistence wrapper designed to make working with persistence solutions like CoreData in a much easier way. Than

Modo 2.1k Dec 9, 2022
A library that provides the ability to import/export Realm files from a variety of data container formats.

Realm Converter Realm Converter is an open source software utility framework to make it easier to get data both in and out of Realm. It has been built

Realm 212 Dec 9, 2022
Sync Realm Database with CloudKit

IceCream helps you sync Realm Database with CloudKit. It works like magic! Features Realm Database Off-line First Thread Safety Reactive Programming O

Soledad 1.8k Jan 6, 2023
Unrealm is an extension on RealmCocoa, which enables Swift native types to be saved in Realm.

Unrealm enables you to easily store Swift native Classes, Structs and Enums into Realm . Stop inheriting from Object! Go for Protocol-Oriented program

Artur  Mkrtchyan 518 Dec 13, 2022
A simple order manager, created in order to try Realm database

Overview A simple order manager, created in order to get acquainted with the features and limitations of the local Realm database. The project is writ

Kirill Sidorov 0 Oct 14, 2021
Creating a Todo app using Realm and SwiftUI

Realmで作るTodoアプリ note記事「【SwiftUI】Realmを使ってTodoアプリを作る」のソースです。

null 1 Jul 20, 2022
MagicData - A replacement of SQlite, CoreData or Realm.

MagicData A replacement of SQlite, CoreData or Realm. It is very easy to use and is a light version. Guides MagicData We use MagicData manage all the

Underthestars-zhy 20 Jul 4, 2022
Realm-powered Core Data persistent store

RealmIncrementalStore Realm-powered Core Data persistent store Wait, what? I like Realm. Realm's memory-mapped DB blows other databases out of the wat

Eureka 227 Jun 24, 2022
Realm Manager for iOS

Microservices for RealmSwift. Split realms for easier management and migration

Lê Xuân Quỳnh 2 Aug 13, 2022
Ios-App-ication-Swift - A simple iOS application made in Xcode using Swift

?? iPhone Calculator A simple iOS application made in Xcode using Swift. This ap

Kushal Shingote 1 Feb 2, 2022
Easy direct access to your database 🎯

OHMySQL ★★ Every star is appreciated! ★★ The library supports Objective-C and Swift, iOS and macOS. You can connect to your remote MySQL database usin

Oleg 210 Dec 28, 2022
Safe and easy wrappers for common Firebase Realtime Database functions.

FirebaseHelper FirebaseHelper is a small wrapper over Firebase's realtime database, providing streamlined methods for get, set, delete, and increment

Quan Vo 15 Apr 9, 2022
Safe and easy wrappers for RealmSwift

RealmWrapper RealmWrapper is wrapper library for RealmSwift in realm-cocoa If you use RealmWrapper, you can easily use UI update through Notification

DongHee Kang 72 Nov 1, 2022
Easy and beautiful way for a user to pick content, files or images. Written in Objective C

##JVTImageFilePicker Description ImageFilesPicker Is a butifuly designed UICompenent for uploading content Preview from recent camera roll Live Camera

Matan Abravanel 60 Jun 19, 2022
Realm RxSwift - This application was written in order to use Realm, RxSwift frameworks in real example

Realm_RxSwift This simple app was written to introduce basic operations of some

Elbek Khasanov 3 Apr 7, 2022
🛰 CoreLocation Made Easy - Efficient & Easy Location Tracker, IP Location, Gecoder, Geofence, Autocomplete, Beacon Ranging, Broadcaster and Visits Monitoring

Location Manager Made Easy SwiftLocation is a lightweight Swift Library that provides an easy way to work with location-related functionalities. No mo

Daniele Margutti 3.2k Dec 30, 2022
🛰 CoreLocation Made Easy - Efficient & Easy Location Tracker, IP Location, Gecoder, Geofence, Autocomplete, Beacon Ranging, Broadcaster and Visits Monitoring

Location Manager Made Easy SwiftLocation is a lightweight Swift Library that provides an easy way to work with location-related functionalities. No mo

Daniele Margutti 3.2k Dec 30, 2022
Clocks made out of clocks made out of code

Clocks I came across this digital clock composed out of a set of analog clocks, created by Humans Since 1982, in a tweet, so I decided to remake it in

Harshil Shah 43 Sep 28, 2022
Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift

EVReflection General information At this moment the master branch is tested with Swift 4.2 and 5.0 beta If you want to continue using EVReflection in

Edwin Vermeer 964 Dec 14, 2022
Realm is a mobile database: a replacement for Core Data & SQLite

Realm is a mobile database that runs directly inside phones, tablets or wearables. This repository holds the source code for the iOS, macOS, tvOS & wa

Realm 15.7k Jan 7, 2023