Skip to content

Commit

Permalink
bug/208 Fixed setting of locale on iOS (#209)
Browse files Browse the repository at this point in the history
* Fixed setting of locale on iOS

* removed ignore warning

* removed old version checks. Adds breaking change.

* Added setting of locale to example app.

* fixed changelog

* fixed typo
  • Loading branch information
TimHoogstrate authored Mar 6, 2024
1 parent 518d8bf commit a536428
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 93 deletions.
14 changes: 11 additions & 3 deletions geocoding_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
## 3.0.0

* **BREAKING CHANGES**:
* Removes the `localeIdentifier` argument from all methods. Use method `setLocaleIdentifier` to configure the locale.
* Removes old iOS version checks and expects iOS 12 and above. (minimal iOS version is 12 per 2.2.0)
* Fixes to configure the locale.
* Updated example app with locale example.

## 2.3.0

* Implements `isPresent` that always returns true.
* Implements `isPresent` that always returns true.

## 2.2.0

* Updates `geocoding_platform_interface` to version 3.1.0.
* Updates minimal iOS version of the example application to 12.
* Updates `geocoding_platform_interface` to version 3.1.0.
* Updates minimal iOS version of the example application to 12.

## 2.1.1

Expand Down
32 changes: 29 additions & 3 deletions geocoding_ios/example/lib/plugin_example/geocode_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
final TextEditingController _latitudeController = TextEditingController();
final TextEditingController _longitudeController = TextEditingController();
String _output = '';
GeocodingIOS _geocodingIOS = GeocodingIOS();

@override
void initState() {
Expand Down Expand Up @@ -74,7 +75,7 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
final latitude = double.parse(_latitudeController.text);
final longitude = double.parse(_longitudeController.text);

GeocodingIOS()
_geocodingIOS
.placemarkFromCoordinates(latitude, longitude)
.then((placemarks) {
var output = 'No results found.';
Expand Down Expand Up @@ -107,7 +108,7 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
child: ElevatedButton(
child: Text('Look up location'),
onPressed: () {
GeocodingIOS()
_geocodingIOS
.locationFromAddress(_addressController.text)
.then((locations) {
var output = 'No results found.';
Expand All @@ -128,7 +129,7 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
child: ElevatedButton(
child: Text('Is present'),
onPressed: () {
GeocodingIOS().isPresent().then((isPresent) {
_geocodingIOS.isPresent().then((isPresent) {
var output = isPresent
? "Geocoder is present"
: "Geocoder is not present";
Expand All @@ -137,6 +138,31 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
});
});
})),
const Padding(
padding: EdgeInsets.only(top: 8),
),
Center(
child: ElevatedButton(
child: Text('Set locale en_US'),
onPressed: () {
_geocodingIOS.setLocaleIdentifier("en_US").then((_) {
setState(() {});
});
})),
const Padding(
padding: EdgeInsets.only(top: 8),
),
Center(
child: ElevatedButton(
child: Text('Set locale nl_NL'),
onPressed: () {
_geocodingIOS.setLocaleIdentifier("nl_NL").then((_) {
setState(() {});
});
})),
const Padding(
padding: EdgeInsets.only(top: 8),
),
Expanded(
child: SingleChildScrollView(
child: Container(
Expand Down
93 changes: 19 additions & 74 deletions geocoding_ios/ios/Classes/GeocodingHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,82 +31,31 @@ - (void) geocodeFromAddress: (NSString *)address
return;
}

if (@available(iOS 11.0, *)) {
[_geocoder geocodeAddressString:address
inRegion:nil
preferredLocale:locale
completionHandler:^(NSArray< CLPlacemark *> *__nullable placemarks, NSError *__nullable error)
{
[GeocodingHandler completeGeocodingWith:placemarks
error:error
success:successHandler
failure:failureHandler];
}];
} else {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSArray<NSString * > *defaultLanguages;

if (locale != nil) {
defaultLanguages = [standardUserDefaults arrayForKey:@"AppleLanguages"];
[standardUserDefaults setValue:[GeocodingHandler languageCode:locale]
forKey:@"AppleLanguages"];
}

[_geocoder geocodeAddressString:address
completionHandler:^(NSArray< CLPlacemark *> *__nullable placemarks, NSError *__nullable error) {

[GeocodingHandler completeGeocodingWith:placemarks
error:error
success:successHandler
failure:failureHandler];

if (locale != nil) {
[standardUserDefaults setValue:defaultLanguages
forKey:@"AppleLanguages"];
}
}];
}

[_geocoder geocodeAddressString:address
inRegion:nil
preferredLocale:locale
completionHandler:^(NSArray< CLPlacemark *> *__nullable placemarks, NSError *__nullable error)
{
[GeocodingHandler completeGeocodingWith:placemarks
error:error
success:successHandler
failure:failureHandler];
}];
return;
}

- (void) geocodeToAddress: (CLLocation *)location
locale: (NSLocale *)locale
success: (GeocodingSuccess)successHandler
failure: (GeocodingFailure)failureHandler {

if (@available(iOS 11.0, *)) {
[_geocoder reverseGeocodeLocation:location
preferredLocale:locale
completionHandler:^(NSArray< CLPlacemark *> *__nullable placemarks, NSError *__nullable error) {
[GeocodingHandler completeGeocodingWith:placemarks
error:error
success:successHandler
failure:failureHandler];
}];

} else {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSArray<NSString * > *defaultLanguages;

if (locale != nil) {
defaultLanguages = [standardUserDefaults arrayForKey:@"AppleLanguages"];
[standardUserDefaults setValue:[GeocodingHandler languageCode:locale]
forKey:@"AppleLanguages"];
}

[_geocoder reverseGeocodeLocation:location
completionHandler:^(NSArray< CLPlacemark *> *__nullable placemarks, NSError *__nullable error) {
[GeocodingHandler completeGeocodingWith:placemarks
error:error
success:successHandler
failure:failureHandler];

if (locale != nil) {
[standardUserDefaults setValue:defaultLanguages forKey:@"AppleLanguages"];
}
}];
}
[_geocoder reverseGeocodeLocation:location
preferredLocale:locale
completionHandler:^(NSArray< CLPlacemark *> *__nullable placemarks, NSError *__nullable error) {
[GeocodingHandler completeGeocodingWith:placemarks
error:error
success:successHandler
failure:failureHandler];
}];
}

+ (void) completeGeocodingWith: (NSArray<CLPlacemark *> *) placemarks
Expand All @@ -128,10 +77,6 @@ + (void) completeGeocodingWith: (NSArray<CLPlacemark *> *) placemarks


+ (NSString *) languageCode:(NSLocale *)locale {
if (@available(iOS 10.0, *)) {
return [locale languageCode];
} else {
return [[locale localeIdentifier] substringToIndex:2];
}
return [locale languageCode];
}
@end
29 changes: 17 additions & 12 deletions geocoding_ios/lib/geocoding_ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@ import 'package:geocoding_platform_interface/geocoding_platform_interface.dart';

const MethodChannel _channel = MethodChannel('flutter.baseflow.com/geocoding');

/// An implementation of [GeocodingPlatform] for Android.
/// An implementation of [GeocodingPlatform] for iOS.
class GeocodingIOS extends GeocodingPlatform {
/// Registers this class as the default instance of [GeocodingPlatform].
static void registerWith() {
GeocodingPlatform.instance = GeocodingIOS();
}

String? _localeIdentifier;

@override
Future<void> setLocaleIdentifier(
String localeIdentifier,
) async {
_localeIdentifier = localeIdentifier;
}

@override
Future<List<Location>> locationFromAddress(
String address, {
String? localeIdentifier,
}) async {
Future<List<Location>> locationFromAddress(String address) async {
final parameters = <String, String>{
'address': address,
};

if (localeIdentifier != null) {
parameters['localeIdentifier'] = localeIdentifier;
if (_localeIdentifier != null) {
parameters['localeIdentifier'] = _localeIdentifier!;
}
try {
final placemarks = await _channel.invokeMethod(
Expand All @@ -40,16 +46,15 @@ class GeocodingIOS extends GeocodingPlatform {
@override
Future<List<Placemark>> placemarkFromCoordinates(
double latitude,
double longitude, {
String? localeIdentifier,
}) async {
double longitude,
) async {
final parameters = <String, dynamic>{
'latitude': latitude,
'longitude': longitude,
};

if (localeIdentifier != null) {
parameters['localeIdentifier'] = localeIdentifier;
if (_localeIdentifier != null) {
parameters['localeIdentifier'] = _localeIdentifier!;
}

final placemarks =
Expand Down
2 changes: 1 addition & 1 deletion geocoding_ios/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: geocoding_ios
description: A Flutter Geocoding plugin which provides easy geocoding and reverse-geocoding features.
version: 2.3.0
version: 3.0.0
repository: https://github.com/baseflow/flutter-geocoding/tree/main/geocoding_ios
issue_tracker: https://github.com/Baseflow/flutter-geocoding/issues

Expand Down

0 comments on commit a536428

Please sign in to comment.