Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions geocoding_darwin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can directly create a new version number and use it here (can be a patch fix, so I suggest using version 1.0.1).


- 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
Expand Down
128 changes: 70 additions & 58 deletions geocoding_darwin/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -32,9 +33,7 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
final TextEditingController _longitudeController = TextEditingController();
String _output = '';
Locale? _locale;
final Geocoding _geocoding = GeocodingDarwinFactory().createGeocoding(
GeocodingDarwinCreationParams(),
);
final cl.CLGeocoder _geocoder = cl.CLGeocoder();

@override
void initState() {
Expand Down Expand Up @@ -119,30 +118,8 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
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)),
Expand All @@ -156,37 +133,17 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
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)),
Expand All @@ -206,16 +163,68 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
],
);
}

cl.Locale? _nativeLocale() {
return _locale != null ? cl.Locale(identifier: _locale!.toString()) : null;
}

Future<void> _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<void> _forwardGeocode() async {
setState(() => _output = 'Loading...');

try {
final placemarks = await _geocoder.geocodeAddressString(
_addressController.text,
_nativeLocale(),
);

var output = 'No results found.';
if (placemarks != null && placemarks.isNotEmpty) {
final cl.CLLocation? location = placemarks[0].location;
output = location != null
? await location.toDisplayString()
: 'No location found.';
}

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,
Expand All @@ -225,11 +234,14 @@ extension _PlacemarkExtensions on Placemark {
}
}

extension _LocationExtensions on Location {
String toDisplayString() {
extension _CLLocationExtensions on cl.CLLocation {
Future<String> toDisplayString() async {
final cl.CLLocationCoordinate2D coordinate = await getCoordinate();
final int timestamp = await getTimestamp();

return '''
Latitude: $latitude,
Longitude: $longitude,
Timestamp: $timestamp''';
Latitude: ${coordinate.latitude},
Longitude: ${coordinate.longitude},
Timestamp: ${DateTime.fromMillisecondsSinceEpoch(timestamp)}''';
}
}