Skip to content
A Swift implementation of the geohash algorithm.
Swift Ruby Objective-C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Geodesy.xcodeproj
Geodesy.xcworkspace
GeodesyTests
Source
Supporting Files
.gitignore
.swift-version
.swiftlint.yml
.travis.yml
Geodesy.podspec
LICENSE.md
README.md

README.md

Geodesy

MIT CocoaPods Swift

A Swift implementation of the geohash algorithm.

Geodesy comes from the ancient Greek word γεωδαισία which means dividing the earth. This is basically what the geohash algorithm does. The hash is just a string of characters, the first divides the earth into 32 separate regions, measured precisely along lines of latitude and longitude. Then next character subdivides a region further into 32 subsections. On and on it goes, each added character more precisely specifying a location on the globe. Nice!

The beauty of the algorithm really shines when you want to filter a list of locations by proximity to a given location. Instead of running some complicated trigonometry on all the points, you can simply compare geohashes. The nearest points will be the ones who share the longest prefix with the given point's geohash.

let currentGeohash = user.location.geohash()
let nearby = db.hotspots.where("geohash", beginsWith: currentGeohash.prefix(8))

Violá. A hash length 8 characters long defines a square roughly 38 meters per side.

Usage

Geodesy is designed to be convenient to use with CoreLocation. There are a number of useful properties added in extensions on CLLocation and CLLocationCoordinate2D.

let location: CLLocation = manager.currentLocation
let geohash = location.geohash(precision: 8) // An eight-character geohash of type String
let neighbors = location.neighbors(precision: 8) // An array of nine eight-character strings representing the eight regions surrounding the original region.

Neighbors?

To handle cases where a location sits near the border of its enclosing region, so that nearby locations potentially are just in the next region over, it is easy to include the eight neighboring regions in addition to actual one.

You can’t perform that action at this time.