diff --git a/geocoding_android/CHANGELOG.md b/geocoding_android/CHANGELOG.md index aab699a..0591170 100644 --- a/geocoding_android/CHANGELOG.md +++ b/geocoding_android/CHANGELOG.md @@ -1,3 +1,11 @@ +## 3.0.0 + +* **BREAKING CHANGES**: + * Updates `geocoding_platform_interface` to version 3.1.0. + * Adds method `setLocaleIdentifier` to set the locale for all calls to the geocoding platform. + * Removes the `localeIdentifier` argument from all methods. Use method `setLocaleIdentifier` to configure the locale. +* Implements `placemarkFromAddress`. + ## 2.1.2 * Downgrades Android Gradle plugin to version 7.3.1 so the project is inline with current Flutter stable (version 3.10.5). diff --git a/geocoding_android/android/src/main/java/com/baseflow/geocoding/Geocoding.java b/geocoding_android/android/src/main/java/com/baseflow/geocoding/Geocoding.java index 16ffd1b..cf3d0a9 100644 --- a/geocoding_android/android/src/main/java/com/baseflow/geocoding/Geocoding.java +++ b/geocoding_android/android/src/main/java/com/baseflow/geocoding/Geocoding.java @@ -12,6 +12,7 @@ /** Geocoding components to lookup address or coordinates. */ class Geocoding { private final Context androidContext; + @Nullable private Locale locale; /** * Uses the given {@code androidContext} to execute geocoding features @@ -22,15 +23,18 @@ class Geocoding { this.androidContext = androidContext; } + void setLocaleIdentifier(@Nullable Locale locale) { + this.locale = locale; + } + /** * Returns a list of Address objects matching the supplied address string. * * @param address the address string for the search - * @param locale the desired Locale for the query results * @return a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available. * @throws java.io.IOException if the network is unavailable or any other I/O problem occurs. */ - List
placemarkFromAddress(String address, Locale locale) throws IOException { + List placemarkFromAddress(String address) throws IOException { final Geocoder geocoder = createGeocoder(androidContext, locale); return geocoder.getFromLocationName(address, 5); @@ -41,15 +45,13 @@ List placemarkFromAddress(String address, Locale locale) throws IOExcep * * @param latitude the latitude point for the search * @param longitude the longitude point for the search - * @param locale the desired Locale for the query results * @return a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available. * @throws IOException if the network is unavailable or any other I/O problem occurs. */ List placemarkFromCoordinates( double latitude, - double longitude, - Locale locale) throws IOException { - + double longitude + ) throws IOException { final Geocoder geocoder = createGeocoder(androidContext, locale); return geocoder.getFromLocation(latitude, longitude, 5); } diff --git a/geocoding_android/android/src/main/java/com/baseflow/geocoding/MethodCallHandlerImpl.java b/geocoding_android/android/src/main/java/com/baseflow/geocoding/MethodCallHandlerImpl.java index 5d55185..7a48ad9 100644 --- a/geocoding_android/android/src/main/java/com/baseflow/geocoding/MethodCallHandlerImpl.java +++ b/geocoding_android/android/src/main/java/com/baseflow/geocoding/MethodCallHandlerImpl.java @@ -3,6 +3,7 @@ import android.location.Address; import android.util.Log; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.baseflow.geocoding.utils.AddressMapper; @@ -10,6 +11,7 @@ import java.io.IOException; import java.util.List; +import java.util.Locale; import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.MethodCall; @@ -36,11 +38,20 @@ final class MethodCallHandlerImpl implements MethodCallHandler { } @Override - public void onMethodCall(final MethodCall call, final Result result) { + public void onMethodCall( + final MethodCall call, + @NonNull final Result result + ) { switch (call.method) { + case "setLocaleIdentifier": + setLocaleIdentifier(call, result); + break; case "locationFromAddress": onLocationFromAddress(call, result); break; + case "placemarkFromAddress": + onPlacemarkFromAddress(call, result); + break; case "placemarkFromCoordinates": onPlacemarkFromCoordinates(call, result); break; @@ -82,9 +93,16 @@ void stopListening() { channel = null; } + private void setLocaleIdentifier(MethodCall call, Result result) { + final String languageTag = call.argument("localeIdentifier"); + + geocoding.setLocaleIdentifier(LocaleConverter.fromLanguageTag(languageTag)); + + result.success(true); + } + private void onLocationFromAddress(MethodCall call, Result result) { final String address = call.argument("address"); - final String languageTag = call.argument("localeIdentifier"); if (address == null || address.isEmpty()) { result.error( @@ -94,9 +112,7 @@ private void onLocationFromAddress(MethodCall call, Result result) { } try { - final List addresses = geocoding.placemarkFromAddress( - address, - LocaleConverter.fromLanguageTag(languageTag)); + final List addresses = geocoding.placemarkFromAddress(address); if (addresses == null || addresses.isEmpty()) { result.error( @@ -110,26 +126,61 @@ private void onLocationFromAddress(MethodCall call, Result result) { } catch (IOException ex) { result.error( "IO_ERROR", - String.format("A network error occurred trying to lookup the address ''.", address), + String.format("A network error occurred trying to lookup the address '%s'.", address), null ); } } + private void onPlacemarkFromAddress(final MethodCall call, final Result result) { + final String address = call.argument("address"); + + if (address == null || address.isEmpty()) { + result.error( + "ARGUMENT_ERROR", + "Supply a valid value for the 'address' parameter.", + null); + } + + try { + final List addresses = geocoding.placemarkFromAddress(address); + + if (addresses == null || addresses.isEmpty()) { + result.error( + "NOT_FOUND", + String.format("No coordinates found for '%s'", address), + null); + return; + } + + result.success(AddressMapper.toAddressHashMapList(addresses)); + } catch (IOException e) { + result.error( + "IO_ERROR", + String.format("A network error occurred trying to lookup the address '%s'.", address), + null + ); + } + } + private void onPlacemarkFromCoordinates(final MethodCall call, final Result result) { final double latitude = call.argument("latitude"); final double longitude = call.argument("longitude"); - final String languageTag = call.argument("localeIdentifier"); try { final List addresses = geocoding.placemarkFromCoordinates( latitude, - longitude, - LocaleConverter.fromLanguageTag(languageTag)); + longitude); + if (addresses == null || addresses.isEmpty()) { result.error( "NOT_FOUND", - String.format("No address information found for supplied coordinates (latitude: %f, longitude: %f).", latitude, longitude), + String.format( + Locale.ENGLISH, + "No address information found for supplied coordinates (latitude: %f, longitude: %f).", + latitude, + longitude + ), null); return; } @@ -137,7 +188,12 @@ private void onPlacemarkFromCoordinates(final MethodCall call, final Result resu } catch (IOException ex) { result.error( "IO_ERROR", - String.format("A network error occurred trying to lookup the supplied coordinates (latitude: %f, longitude: %f).", latitude, longitude), + String.format( + Locale.ENGLISH, + "A network error occurred trying to lookup the supplied coordinates (latitude: %f, longitude: %f).", + latitude, + longitude + ), null ); } diff --git a/geocoding_android/example/lib/plugin_example/geocode_page.dart b/geocoding_android/example/lib/plugin_example/geocode_page.dart index 461cd1a..504bc0a 100644 --- a/geocoding_android/example/lib/plugin_example/geocode_page.dart +++ b/geocoding_android/example/lib/plugin_example/geocode_page.dart @@ -103,6 +103,39 @@ class _GeocodeWidgetState extends State