geohash actor

This commit is contained in:
randogoth 2026-06-07 16:51:14 +03:00
parent f1e54f2722
commit cabac5ec49
4 changed files with 193 additions and 0 deletions

69
lib/src/geo_actor.dart Normal file
View file

@ -0,0 +1,69 @@
import 'package:actors/actors.dart';
import 'package:dart_geohash/dart_geohash.dart';
sealed class GeoMessage {
const GeoMessage();
}
final class EncodeLocation extends GeoMessage {
final double latitude;
final double longitude;
final int precision;
const EncodeLocation({
required this.latitude,
required this.longitude,
this.precision = 9,
});
}
final class DecodeGeohash extends GeoMessage {
final String geohash;
const DecodeGeohash(this.geohash);
}
final class NeighborsOf extends GeoMessage {
final String geohash;
const NeighborsOf(this.geohash);
}
sealed class GeoResult {
const GeoResult();
}
final class GeohashEncoded extends GeoResult {
final String geohash;
const GeohashEncoded(this.geohash);
}
final class LocationDecoded extends GeoResult {
final double latitude;
final double longitude;
const LocationDecoded({required this.latitude, required this.longitude});
}
// Keys: NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST, CENTRAL
final class NeighborsResult extends GeoResult {
final Map<String, String> neighbors;
const NeighborsResult(this.neighbors);
}
class GeoActor with Handler<GeoMessage, GeoResult> {
final _hasher = GeoHasher();
@override
Future<GeoResult> handle(GeoMessage message) => switch (message) {
EncodeLocation(:final latitude, :final longitude, :final precision) =>
Future.value(
GeohashEncoded(
_hasher.encode(longitude, latitude, precision: precision)),
),
DecodeGeohash(:final geohash) => Future.value(_decode(geohash)),
NeighborsOf(:final geohash) =>
Future.value(NeighborsResult(_hasher.neighbors(geohash))),
};
LocationDecoded _decode(String geohash) {
final coords = _hasher.decode(geohash); // [longitude, latitude]
return LocationDecoded(latitude: coords[1], longitude: coords[0]);
}
}