From 1bb08343937aa6cdcbdbe7c6819c2f9d6ed5dacf Mon Sep 17 00:00:00 2001 From: Rick van Dijk Date: Thu, 25 Jun 2026 11:57:43 -0700 Subject: [PATCH 1/4] Use native CLGeocoder in geocoding_darwin example. Demonstrates the external native API via clgeocoder.dart instead of the shared Geocoding platform interface, matching the geocoding_android example pattern. --- geocoding_darwin/example/lib/main.dart | 132 ++++++++++++++----------- 1 file changed, 73 insertions(+), 59 deletions(-) diff --git a/geocoding_darwin/example/lib/main.dart b/geocoding_darwin/example/lib/main.dart index cfaabb1..ffdac12 100644 --- a/geocoding_darwin/example/lib/main.dart +++ b/geocoding_darwin/example/lib/main.dart @@ -1,6 +1,7 @@ import 'package:baseflow_plugin_template/baseflow_plugin_template.dart'; import 'package:flutter/material.dart'; -import 'package:geocoding_darwin/geocoding_darwin.dart'; +import 'package:flutter/services.dart'; +import 'package:geocoding_darwin/clgeocoder.dart' as cl; /// Defines the main theme color. final MaterialColor themeMaterialColor = @@ -32,9 +33,11 @@ class _GeocodeWidgetState extends State { final TextEditingController _longitudeController = TextEditingController(); String _output = ''; Locale? _locale; - final Geocoding _geocoding = GeocodingDarwinFactory().createGeocoding( - GeocodingDarwinCreationParams(), - ); + final cl.CLGeocoder _geocoder = cl.CLGeocoder(); + + cl.Locale? _nativeLocale() { + return _locale != null ? cl.Locale(identifier: _locale!.toString()) : null; + } @override void initState() { @@ -119,30 +122,8 @@ class _GeocodeWidgetState extends State { const Padding(padding: EdgeInsets.only(top: 8)), Center( child: ElevatedButton( + onPressed: _reverseGeocode, child: const Text('Look up address'), - onPressed: () { - final latitude = double.parse(_latitudeController.text); - final longitude = double.parse( - _longitudeController.text, - ); - - _geocoding - .placemarkFromCoordinates( - latitude, - longitude, - locale: _locale, - ) - .then((placemarks) { - var output = 'No results found.'; - if (placemarks.isNotEmpty) { - output = placemarks[0].toDisplayString(); - } - - setState(() { - _output = output; - }); - }); - }, ), ), const Padding(padding: EdgeInsets.only(top: 32)), @@ -156,37 +137,17 @@ class _GeocodeWidgetState extends State { const Padding(padding: EdgeInsets.only(top: 8)), Center( child: ElevatedButton( + onPressed: _forwardGeocode, child: const Text('Look up location'), - onPressed: () { - _geocoding - .locationFromAddress(_addressController.text) - .then((locations) { - var output = 'No results found.'; - if (locations.isNotEmpty) { - output = locations[0].toDisplayString(); - } - - setState(() { - _output = output; - }); - }); - }, ), ), const Padding(padding: EdgeInsets.only(top: 8)), Center( child: ElevatedButton( - child: const Text('Is present'), onPressed: () { - _geocoding.isPresent().then((isPresent) { - var output = isPresent - ? "Geocoder is present" - : "Geocoder is not present"; - setState(() { - _output = output; - }); - }); + setState(() => _output = 'Geocoder is present'); }, + child: const Text('Is present'), ), ), const Padding(padding: EdgeInsets.only(top: 8)), @@ -206,16 +167,61 @@ class _GeocodeWidgetState extends State { ], ); } + + Future _reverseGeocode() async { + setState(() => _output = 'Loading...'); + + try { + final latitude = double.parse(_latitudeController.text); + final longitude = double.parse(_longitudeController.text); + + final placemarks = await _geocoder.reverseGeocodeLocation( + cl.CLLocation(latitude: latitude, longitude: longitude), + _nativeLocale(), + ); + + var output = 'No results found.'; + if (placemarks != null && placemarks.isNotEmpty) { + output = placemarks[0].toDisplayString(); + } + + setState(() => _output = output); + } on PlatformException catch (e) { + setState(() => _output = 'Error: ${e.message ?? e.code}'); + } on FormatException catch (e) { + setState(() => _output = 'Error: ${e.message}'); + } + } + + Future _forwardGeocode() async { + setState(() => _output = 'Loading...'); + + try { + final placemarks = await _geocoder.geocodeAddressString( + _addressController.text, + _nativeLocale(), + ); + + var output = 'No results found.'; + if (placemarks != null && placemarks.isNotEmpty) { + output = await placemarks[0].toLocationDisplayString(); + } + + setState(() => _output = output); + } on PlatformException catch (e) { + setState(() => _output = 'Error: ${e.message ?? e.code}'); + } + } } -extension _PlacemarkExtensions on Placemark { +extension _CLPlacemarkExtensions on cl.CLPlacemark { String toDisplayString() { return ''' Name: $name, - Street: $street, + Street: ${postalAddress?.street ?? thoroughfare}, ISO Country Code: $isoCountryCode, Country: $country, - Postal code: $postalCode, + Postal code: ${postalAddress?.postalCode ?? postalCode}, Administrative area: $administrativeArea, Subadministrative area: $subAdministrativeArea, Locality: $locality, @@ -223,13 +229,21 @@ extension _PlacemarkExtensions on Placemark { Thoroughfare: $thoroughfare, Subthoroughfare: $subThoroughfare'''; } -} -extension _LocationExtensions on Location { - String toDisplayString() { + Future toLocationDisplayString() async { + final cl.CLLocation? localLocation = location; + + if (localLocation == null) { + return 'No location found.'; + } + + final cl.CLLocationCoordinate2D coordinate = await localLocation + .getCoordinate(); + final int timestamp = await localLocation.getTimestamp(); + return ''' - Latitude: $latitude, - Longitude: $longitude, - Timestamp: $timestamp'''; + Latitude: ${coordinate.latitude}, + Longitude: ${coordinate.longitude}, + Timestamp: ${DateTime.fromMillisecondsSinceEpoch(timestamp)}'''; } } From 7abbcf9a11fbdac6b4cb9f43c85eba62d4c394ff Mon Sep 17 00:00:00 2001 From: Rick van Dijk Date: Thu, 25 Jun 2026 12:13:58 -0700 Subject: [PATCH 2/4] Changed to using extension on location --- geocoding_darwin/example/lib/main.dart | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/geocoding_darwin/example/lib/main.dart b/geocoding_darwin/example/lib/main.dart index ffdac12..4431cba 100644 --- a/geocoding_darwin/example/lib/main.dart +++ b/geocoding_darwin/example/lib/main.dart @@ -204,7 +204,10 @@ class _GeocodeWidgetState extends State { var output = 'No results found.'; if (placemarks != null && placemarks.isNotEmpty) { - output = await placemarks[0].toLocationDisplayString(); + final cl.CLLocation? location = placemarks[0].location; + output = location != null + ? await location.toDisplayString() + : 'No location found.'; } setState(() => _output = output); @@ -229,17 +232,12 @@ extension _CLPlacemarkExtensions on cl.CLPlacemark { Thoroughfare: $thoroughfare, Subthoroughfare: $subThoroughfare'''; } +} - Future toLocationDisplayString() async { - final cl.CLLocation? localLocation = location; - - if (localLocation == null) { - return 'No location found.'; - } - - final cl.CLLocationCoordinate2D coordinate = await localLocation - .getCoordinate(); - final int timestamp = await localLocation.getTimestamp(); +extension _CLLocationExtensions on cl.CLLocation { + Future toDisplayString() async { + final cl.CLLocationCoordinate2D coordinate = await getCoordinate(); + final int timestamp = await getTimestamp(); return ''' Latitude: ${coordinate.latitude}, From db9f0c99313e2ef92209128c1e1edc3bda981b93 Mon Sep 17 00:00:00 2001 From: Rick van Dijk Date: Thu, 25 Jun 2026 14:01:23 -0700 Subject: [PATCH 3/4] moved down private function --- geocoding_darwin/example/lib/main.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/geocoding_darwin/example/lib/main.dart b/geocoding_darwin/example/lib/main.dart index 4431cba..cfbb471 100644 --- a/geocoding_darwin/example/lib/main.dart +++ b/geocoding_darwin/example/lib/main.dart @@ -35,10 +35,6 @@ class _GeocodeWidgetState extends State { Locale? _locale; final cl.CLGeocoder _geocoder = cl.CLGeocoder(); - cl.Locale? _nativeLocale() { - return _locale != null ? cl.Locale(identifier: _locale!.toString()) : null; - } - @override void initState() { _addressController.text = 'Gronausestraat 710, Enschede'; @@ -168,6 +164,10 @@ class _GeocodeWidgetState extends State { ); } + cl.Locale? _nativeLocale() { + return _locale != null ? cl.Locale(identifier: _locale!.toString()) : null; + } + Future _reverseGeocode() async { setState(() => _output = 'Loading...'); From 330ef7a52d8ac25cf69b95331bceafeafe94d9d7 Mon Sep 17 00:00:00 2001 From: Rick van Dijk Date: Thu, 25 Jun 2026 14:26:46 -0700 Subject: [PATCH 4/4] updated changelog --- geocoding_darwin/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/geocoding_darwin/CHANGELOG.md b/geocoding_darwin/CHANGELOG.md index bad65f4..c5847fc 100644 --- a/geocoding_darwin/CHANGELOG.md +++ b/geocoding_darwin/CHANGELOG.md @@ -1,3 +1,8 @@ +## Unreleased + +- Updates the example app to demonstrate Darwin-specific native CLGeocoder + methods via `clgeocoder.dart`. + ## 1.0.0 - Initial release of the geocoding_darwin package containing easy geocoding and