Skip to content

Commit 24ec8d6

Browse files
rickdijkRick van Dijk
andauthored
Use native CLGeocoder in geocoding_darwin example (#302)
* 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. * Changed to using extension on location * moved down private function * updated changelog * Updated documentation regarding contribution to only one package and versioning * Version bump for geocoding_darwin package --------- Co-authored-by: Rick van Dijk <rick@baseflow.com>
1 parent 9e206c3 commit 24ec8d6

6 files changed

Lines changed: 98 additions & 64 deletions

File tree

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
- [ ] I made sure the project builds.
88
- [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
9-
- [ ] I updated `pubspec.yaml` with an appropriate new version according to the [pub versioning philosophy], or this PR is does not need version changes.
10-
- [ ] I updated `CHANGELOG.md` to add a description of the change.
9+
- [ ] This PR only changes one package (or documents why an exception is needed).
10+
- [ ] I updated `pubspec.yaml` with an appropriate new version according to the [pub versioning philosophy], or this PR does not need version changes.
11+
- [ ] I updated `CHANGELOG.md` with a new `## x.y.z` section that matches the version in `pubspec.yaml`
1112
- [ ] I updated/added relevant documentation (doc comments with `///`).
1213
- [ ] I rebased onto `main`.
1314
- [ ] I added new tests to check the change I am making, or this PR does not need tests.

CONTRIBUTING.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Setting up your development environment
2323
Running the example project
2424
---------------------------
2525

26-
* Change into the example directory: `cd example`
26+
* Change into the example directory for the package you are working on, e.g. `cd geocoding/example`
2727
* Run the App: `flutter run`
2828

2929
Contribute
@@ -35,7 +35,7 @@ We really appreciate contributions via GitHub pull requests. To contribute take
3535
* `git fetch upstream`
3636
* `git checkout upstream/main -b <name_of_your_branch>`
3737
* Apply your changes
38-
* Verify your changes and fix potential warnings/ errors:
38+
* Verify your changes and fix potential warnings/ errors (run from the package you changed):
3939
* Check formatting: `flutter format .`
4040
* Run static analyses: `flutter analyze`
4141
* Run unit-tests: `flutter test`
@@ -46,4 +46,14 @@ Send us your pull request:
4646

4747
* Go to `https://github.com/Baseflow/flutter-geocoding` and click the "Compare & pull request" button.
4848

49-
Please make sure you solved all warnings and errors reported by the static code analyses and that you fill in the full pull request template. Failing to do so will result in us asking you to fix it.
49+
Please make sure you solved all warnings and errors reported by the static code analyses and that you fill in the full pull request template. Failing to do so will result in us asking you to fix it.
50+
51+
Pull request scope and versioning
52+
---------------------------------
53+
54+
Each pull request should follow these conventions:
55+
56+
* **One package per PR** — keep changes confined to a single package directory (`geocoding/`, `geocoding_android/`, `geocoding_darwin/`, etc.). Cross-package changes require maintainer coordination and are usually split into separate PRs.
57+
* **Version bump required** — when a PR changes anything that would be published to pub.dev (code, example, README in the package, etc.), bump the `version:` field in that package's `pubspec.yaml` according to the [pub versioning philosophy](https://dart.dev/tools/pub/versioning).
58+
* **Matching CHANGELOG entry** — add a new top section in that package's `CHANGELOG.md` using the exact version number (e.g. `## 1.0.1`). The version in `pubspec.yaml` and the CHANGELOG heading must match. Describe the change in the same style as existing entries (see `geocoding_android/CHANGELOG.md` version `5.0.1` as a reference for example-only changes).
59+
* **When no version bump is needed** — root-only documentation, CI, or `.github/` changes that do not affect a published package do not require a version bump. Note this in the pull request template when applicable.

geocoding_darwin/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.0.1
2+
3+
- Updates the example app to demonstrate Darwin-specific native CLGeocoder
4+
methods via `clgeocoder.dart`.
5+
16
## 1.0.0
27

38
- Initial release of the geocoding_darwin package containing easy geocoding and

geocoding_darwin/CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Please start by taking a look at the general guide to contributing to the `baseflow/flutter-geocoding` repo:
44
https://github.com/baseflow/flutter-geocoding/blob/main/CONTRIBUTING.md
55

6+
## Versioning
7+
8+
When your change affects this package (code, example, README, etc.), bump the version in
9+
[`pubspec.yaml`](pubspec.yaml) and add a matching entry at the top of
10+
[`CHANGELOG.md`](CHANGELOG.md) using the same version number (e.g. `## 1.0.1`). See the [Pull request scope and versioning](https://github.com/baseflow/flutter-geocoding/blob/main/CONTRIBUTING.md#pull-request-scope-and-versioning) section in the root contributing guide.
11+
612
## Package Structure
713

814
This plugin serves as a platform implementation plugin as outlined in [federated plugins](https://docs.flutter.dev/packages-and-plugins/developing-packages#federated-plugins).

geocoding_darwin/example/lib/main.dart

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:baseflow_plugin_template/baseflow_plugin_template.dart';
22
import 'package:flutter/material.dart';
3-
import 'package:geocoding_darwin/geocoding_darwin.dart';
3+
import 'package:flutter/services.dart';
4+
import 'package:geocoding_darwin/clgeocoder.dart' as cl;
45

56
/// Defines the main theme color.
67
final MaterialColor themeMaterialColor =
@@ -32,9 +33,7 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
3233
final TextEditingController _longitudeController = TextEditingController();
3334
String _output = '';
3435
Locale? _locale;
35-
final Geocoding _geocoding = GeocodingDarwinFactory().createGeocoding(
36-
GeocodingDarwinCreationParams(),
37-
);
36+
final cl.CLGeocoder _geocoder = cl.CLGeocoder();
3837

3938
@override
4039
void initState() {
@@ -119,30 +118,8 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
119118
const Padding(padding: EdgeInsets.only(top: 8)),
120119
Center(
121120
child: ElevatedButton(
121+
onPressed: _reverseGeocode,
122122
child: const Text('Look up address'),
123-
onPressed: () {
124-
final latitude = double.parse(_latitudeController.text);
125-
final longitude = double.parse(
126-
_longitudeController.text,
127-
);
128-
129-
_geocoding
130-
.placemarkFromCoordinates(
131-
latitude,
132-
longitude,
133-
locale: _locale,
134-
)
135-
.then((placemarks) {
136-
var output = 'No results found.';
137-
if (placemarks.isNotEmpty) {
138-
output = placemarks[0].toDisplayString();
139-
}
140-
141-
setState(() {
142-
_output = output;
143-
});
144-
});
145-
},
146123
),
147124
),
148125
const Padding(padding: EdgeInsets.only(top: 32)),
@@ -156,37 +133,17 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
156133
const Padding(padding: EdgeInsets.only(top: 8)),
157134
Center(
158135
child: ElevatedButton(
136+
onPressed: _forwardGeocode,
159137
child: const Text('Look up location'),
160-
onPressed: () {
161-
_geocoding
162-
.locationFromAddress(_addressController.text)
163-
.then((locations) {
164-
var output = 'No results found.';
165-
if (locations.isNotEmpty) {
166-
output = locations[0].toDisplayString();
167-
}
168-
169-
setState(() {
170-
_output = output;
171-
});
172-
});
173-
},
174138
),
175139
),
176140
const Padding(padding: EdgeInsets.only(top: 8)),
177141
Center(
178142
child: ElevatedButton(
179-
child: const Text('Is present'),
180143
onPressed: () {
181-
_geocoding.isPresent().then((isPresent) {
182-
var output = isPresent
183-
? "Geocoder is present"
184-
: "Geocoder is not present";
185-
setState(() {
186-
_output = output;
187-
});
188-
});
144+
setState(() => _output = 'Geocoder is present');
189145
},
146+
child: const Text('Is present'),
190147
),
191148
),
192149
const Padding(padding: EdgeInsets.only(top: 8)),
@@ -206,16 +163,68 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
206163
],
207164
);
208165
}
166+
167+
cl.Locale? _nativeLocale() {
168+
return _locale != null ? cl.Locale(identifier: _locale!.toString()) : null;
169+
}
170+
171+
Future<void> _reverseGeocode() async {
172+
setState(() => _output = 'Loading...');
173+
174+
try {
175+
final latitude = double.parse(_latitudeController.text);
176+
final longitude = double.parse(_longitudeController.text);
177+
178+
final placemarks = await _geocoder.reverseGeocodeLocation(
179+
cl.CLLocation(latitude: latitude, longitude: longitude),
180+
_nativeLocale(),
181+
);
182+
183+
var output = 'No results found.';
184+
if (placemarks != null && placemarks.isNotEmpty) {
185+
output = placemarks[0].toDisplayString();
186+
}
187+
188+
setState(() => _output = output);
189+
} on PlatformException catch (e) {
190+
setState(() => _output = 'Error: ${e.message ?? e.code}');
191+
} on FormatException catch (e) {
192+
setState(() => _output = 'Error: ${e.message}');
193+
}
194+
}
195+
196+
Future<void> _forwardGeocode() async {
197+
setState(() => _output = 'Loading...');
198+
199+
try {
200+
final placemarks = await _geocoder.geocodeAddressString(
201+
_addressController.text,
202+
_nativeLocale(),
203+
);
204+
205+
var output = 'No results found.';
206+
if (placemarks != null && placemarks.isNotEmpty) {
207+
final cl.CLLocation? location = placemarks[0].location;
208+
output = location != null
209+
? await location.toDisplayString()
210+
: 'No location found.';
211+
}
212+
213+
setState(() => _output = output);
214+
} on PlatformException catch (e) {
215+
setState(() => _output = 'Error: ${e.message ?? e.code}');
216+
}
217+
}
209218
}
210219

211-
extension _PlacemarkExtensions on Placemark {
220+
extension _CLPlacemarkExtensions on cl.CLPlacemark {
212221
String toDisplayString() {
213222
return '''
214223
Name: $name,
215-
Street: $street,
224+
Street: ${postalAddress?.street ?? thoroughfare},
216225
ISO Country Code: $isoCountryCode,
217226
Country: $country,
218-
Postal code: $postalCode,
227+
Postal code: ${postalAddress?.postalCode ?? postalCode},
219228
Administrative area: $administrativeArea,
220229
Subadministrative area: $subAdministrativeArea,
221230
Locality: $locality,
@@ -225,11 +234,14 @@ extension _PlacemarkExtensions on Placemark {
225234
}
226235
}
227236

228-
extension _LocationExtensions on Location {
229-
String toDisplayString() {
237+
extension _CLLocationExtensions on cl.CLLocation {
238+
Future<String> toDisplayString() async {
239+
final cl.CLLocationCoordinate2D coordinate = await getCoordinate();
240+
final int timestamp = await getTimestamp();
241+
230242
return '''
231-
Latitude: $latitude,
232-
Longitude: $longitude,
233-
Timestamp: $timestamp''';
243+
Latitude: ${coordinate.latitude},
244+
Longitude: ${coordinate.longitude},
245+
Timestamp: ${DateTime.fromMillisecondsSinceEpoch(timestamp)}''';
234246
}
235247
}

geocoding_darwin/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: geocoding_darwin
22
description: |-
33
A Flutter Geocoding plugin which provides easy geocoding and
44
reverse-geocoding features for iOS and macOS.
5-
version: 1.0.0
5+
version: 1.0.1
66
repository: https://github.com/baseflow/flutter-geocoding
77

88
environment:

0 commit comments

Comments
 (0)