aves/lib/services/geocoding_service.dart
Thibault Deckers 87fb9c73e1 geocoding with locale
removed check workflow while on flutter master
2021-03-10 16:45:39 +09:00

58 lines
1.8 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:latlong/latlong.dart';
class GeocodingService {
static const platform = MethodChannel('deckers.thibault/aves/geocoding');
// geocoding requires Google Play Services
static Future<List<Address>> getAddress(LatLng coordinates, String locale) async {
try {
final result = await platform.invokeMethod('getAddress', <String, dynamic>{
'latitude': coordinates.latitude,
'longitude': coordinates.longitude,
'locale': locale,
});
return (result as List).cast<Map>().map((map) => Address.fromMap(map)).toList();
} on PlatformException catch (e) {
debugPrint('getAddress failed with code=${e.code}, exception=${e.message}, details=${e.details}}');
}
return [];
}
}
@immutable
class Address {
final String 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'],
);
}