georange
georange is a package that helps with encoding geohashes, decoding geohashes,calculating distance between 2 points and generating latitudinal and longitudinal ranges as geohashes to help with the querying of databases (tested on firestore only).
getting started
you should ensure that you add georange as a dependency in your flutter project.
dependencies:
georange: <latest-version>
you should then run flutter packages get
example
there is a detailed example project in the example
folder.
initialize
import georange
to your dart file and initialize
import 'package:georange/georange.dart';
georange georange = georange();
encode latlng
this method encodes the latitude and longitude
var encoded = georange.encode(-1.2862368,36.8195783);
print(encoded);
prints kzf0tvg5n
decode geohash
decode a [geohash] into a pair of latitude and longitude.
point decoded = georange.decode("kzf0tvg5n");
print(decoded);
prints
-1.2862372398376465
36.819584369659424
generate range
range range = georange.geohashrange(-1.2921, 36.8219, distance: 10);
print(range.lower);
print(range.upper);
prints
kzf05k6hh
kzf30mptu
calculate distance between 2 points
point point1 = point(latitude: -4.0435, longitude: 39.6682); //mombasa
point point2 = point(latitude: -1.2921, longitude: 36.8219); // nairobi
var distance = georange.distance(point1, point2);
print(distance);
prints
439.716
distance in kilometres
usage with firestore
- add a document to firestore with a
geohash
field or a different name
final firebasefirestore _db;
...
string myhash = georange.encode(-1.2862368,36.8195783);
await _db.collection("locations").add({
"geohash":myhash,
})
...
- query firestore (runs like a normal firestore query)
final firebasefirestore _db;
georange georange = georange();
range range = georange.geohashrange(currentlocation.latitude, currentlocation.longitude, distance:10);
querysnapshot snapshot = await _db
.collection("locations")
.where("geohash", isgreaterthanorequalto: range.lower)
.where("geohash", islessthanorequalto: range.upper)
.limit(10)
.get();
Comments are closed.