summaryrefslogtreecommitdiff
path: root/lib/services/geocoding_service.dart
diff options
context:
space:
mode:
authormirror <mirror@localhost>2026-11-22 08:41:00 +0900
committermirror <mirror@localhost>2026-11-22 08:41:00 +0900
commit449d8b74bd6d9dae98a07254cdfad677b920e7b0 (patch)
treec131c3528c4d517f56e19b854a7453fedb097d40 /lib/services/geocoding_service.dart
downloadaves-src-master.tar.gz
aves-src-master.zip
snapshot of aves v1.13.12 (secrets removed: android/app/google-services.json)HEADv1.13.12master
Diffstat (limited to 'lib/services/geocoding_service.dart')
-rw-r--r--lib/services/geocoding_service.dart66
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/services/geocoding_service.dart b/lib/services/geocoding_service.dart
new file mode 100644
index 0000000..c5b1133
--- /dev/null
+++ b/lib/services/geocoding_service.dart
@@ -0,0 +1,66 @@
+import 'dart:async';
+import 'dart:ui';
+
+import 'package:equatable/equatable.dart';
+import 'package:flutter/foundation.dart';
+import 'package:flutter/services.dart';
+import 'package:latlong2/latlong.dart';
+import 'package:aves/services/common/channel.dart';
+
+class GeocodingService {
+ static const _platform = AvesMethodChannel('deckers.thibault/aves/geocoding');
+
+ // geocoding requires Google Play Services
+ static Future<List<Address>> getAddress(LatLng coordinates, Locale locale) async {
+ try {
+ final result = await _platform.invokeMethod('getAddress', <String, dynamic>{
+ 'latitude': coordinates.latitude,
+ 'longitude': coordinates.longitude,
+ 'localeLanguageTag': locale.toLanguageTag(),
+ // we only really need one address, but sometimes the native geocoder
+ // returns nothing with `maxResults` of 1, but succeeds with `maxResults` of 2+
+ 'maxResults': 2,
+ });
+ return (result as List).cast<Map>().map(Address.fromMap).toList();
+ } on PlatformException catch (_) {
+ // do not report
+ }
+ return [];
+ }
+}
+
+@immutable
+class Address extends Equatable {
+ final String? addressLine, adminArea, countryCode, countryName, featureName, locality, postalCode, subAdminArea, subLocality, subThoroughfare, thoroughfare;
+
+ @override
+ List<Object?> get props => [addressLine, adminArea, countryCode, countryName, featureName, locality, postalCode, subAdminArea, subLocality, subThoroughfare, thoroughfare];
+
+ const Address({
+ this.addressLine,
+ this.adminArea,
+ this.countryCode,
+ this.countryName,
+ this.featureName,
+ this.locality,
+ this.postalCode,
+ this.subAdminArea,
+ this.subLocality,
+ this.subThoroughfare,
+ this.thoroughfare,
+ });
+
+ factory Address.fromMap(Map map) => Address(
+ addressLine: map['addressLine'],
+ adminArea: map['adminArea'],
+ countryCode: map['countryCode'],
+ countryName: map['countryName'],
+ featureName: map['featureName'],
+ locality: map['locality'],
+ postalCode: map['postalCode'],
+ subAdminArea: map['subAdminArea'],
+ subLocality: map['subLocality'],
+ subThoroughfare: map['subThoroughfare'],
+ thoroughfare: map['thoroughfare'],
+ );
+}