Skip to content

Commit 59df138

Browse files
committed
Use PI 3.1.0 and support placemarkFromAddress
1 parent 128218b commit 59df138

File tree

8 files changed

+156
-34
lines changed

8 files changed

+156
-34
lines changed

geocoding_android/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 3.0.0
2+
3+
* **BREAKING CHANGES**:
4+
* Updates `geocoding_platform_interface` to version 3.1.0.
5+
* Adds method `setLocaleIdentifier` to set the locale for all calls to the geocoding platform.
6+
* Removes the `localeIdentifier` argument from all methods. Use method `setLocaleIdentifier` to configure the locale.
7+
* Implements `placemarkFromAddress`.
8+
19
## 2.1.2
210

311
* Downgrades Android Gradle plugin to version 7.3.1 so the project is inline with current Flutter stable (version 3.10.5).

geocoding_android/android/src/main/java/com/baseflow/geocoding/Geocoding.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/** Geocoding components to lookup address or coordinates. */
1313
class Geocoding {
1414
private final Context androidContext;
15+
@Nullable private Locale locale;
1516

1617
/**
1718
* Uses the given {@code androidContext} to execute geocoding features
@@ -22,15 +23,18 @@ class Geocoding {
2223
this.androidContext = androidContext;
2324
}
2425

26+
void setLocaleIdentifier(@Nullable Locale locale) {
27+
this.locale = locale;
28+
}
29+
2530
/**
2631
* Returns a list of Address objects matching the supplied address string.
2732
*
2833
* @param address the address string for the search
29-
* @param locale the desired Locale for the query results
3034
* @return a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
3135
* @throws java.io.IOException if the network is unavailable or any other I/O problem occurs.
3236
*/
33-
List<Address> placemarkFromAddress(String address, Locale locale) throws IOException {
37+
List<Address> placemarkFromAddress(String address) throws IOException {
3438

3539
final Geocoder geocoder = createGeocoder(androidContext, locale);
3640
return geocoder.getFromLocationName(address, 5);
@@ -41,15 +45,13 @@ List<Address> placemarkFromAddress(String address, Locale locale) throws IOExcep
4145
*
4246
* @param latitude the latitude point for the search
4347
* @param longitude the longitude point for the search
44-
* @param locale the desired Locale for the query results
4548
* @return a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
4649
* @throws IOException if the network is unavailable or any other I/O problem occurs.
4750
*/
4851
List<Address> placemarkFromCoordinates(
4952
double latitude,
50-
double longitude,
51-
Locale locale) throws IOException {
52-
53+
double longitude
54+
) throws IOException {
5355
final Geocoder geocoder = createGeocoder(androidContext, locale);
5456
return geocoder.getFromLocation(latitude, longitude, 5);
5557
}

geocoding_android/android/src/main/java/com/baseflow/geocoding/MethodCallHandlerImpl.java

+67-11
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import android.location.Address;
44
import android.util.Log;
55

6+
import androidx.annotation.NonNull;
67
import androidx.annotation.Nullable;
78

89
import com.baseflow.geocoding.utils.AddressMapper;
910
import com.baseflow.geocoding.utils.LocaleConverter;
1011

1112
import java.io.IOException;
1213
import java.util.List;
14+
import java.util.Locale;
1315

1416
import io.flutter.plugin.common.BinaryMessenger;
1517
import io.flutter.plugin.common.MethodCall;
@@ -36,11 +38,20 @@ final class MethodCallHandlerImpl implements MethodCallHandler {
3638
}
3739

3840
@Override
39-
public void onMethodCall(final MethodCall call, final Result result) {
41+
public void onMethodCall(
42+
final MethodCall call,
43+
@NonNull final Result result
44+
) {
4045
switch (call.method) {
46+
case "setLocaleIdentifier":
47+
setLocaleIdentifier(call, result);
48+
break;
4149
case "locationFromAddress":
4250
onLocationFromAddress(call, result);
4351
break;
52+
case "placemarkFromAddress":
53+
onPlacemarkFromAddress(call, result);
54+
break;
4455
case "placemarkFromCoordinates":
4556
onPlacemarkFromCoordinates(call, result);
4657
break;
@@ -82,9 +93,16 @@ void stopListening() {
8293
channel = null;
8394
}
8495

96+
private void setLocaleIdentifier(MethodCall call, Result result) {
97+
final String languageTag = call.argument("localeIdentifier");
98+
99+
geocoding.setLocaleIdentifier(LocaleConverter.fromLanguageTag(languageTag));
100+
101+
result.success(true);
102+
}
103+
85104
private void onLocationFromAddress(MethodCall call, Result result) {
86105
final String address = call.argument("address");
87-
final String languageTag = call.argument("localeIdentifier");
88106

89107
if (address == null || address.isEmpty()) {
90108
result.error(
@@ -94,9 +112,7 @@ private void onLocationFromAddress(MethodCall call, Result result) {
94112
}
95113

96114
try {
97-
final List<Address> addresses = geocoding.placemarkFromAddress(
98-
address,
99-
LocaleConverter.fromLanguageTag(languageTag));
115+
final List<Address> addresses = geocoding.placemarkFromAddress(address);
100116

101117
if (addresses == null || addresses.isEmpty()) {
102118
result.error(
@@ -110,34 +126,74 @@ private void onLocationFromAddress(MethodCall call, Result result) {
110126
} catch (IOException ex) {
111127
result.error(
112128
"IO_ERROR",
113-
String.format("A network error occurred trying to lookup the address ''.", address),
129+
String.format("A network error occurred trying to lookup the address '%s'.", address),
114130
null
115131
);
116132
}
117133
}
118134

135+
private void onPlacemarkFromAddress(final MethodCall call, final Result result) {
136+
final String address = call.argument("address");
137+
138+
if (address == null || address.isEmpty()) {
139+
result.error(
140+
"ARGUMENT_ERROR",
141+
"Supply a valid value for the 'address' parameter.",
142+
null);
143+
}
144+
145+
try {
146+
final List<Address> addresses = geocoding.placemarkFromAddress(address);
147+
148+
if (addresses == null || addresses.isEmpty()) {
149+
result.error(
150+
"NOT_FOUND",
151+
String.format("No coordinates found for '%s'", address),
152+
null);
153+
return;
154+
}
155+
156+
result.success(AddressMapper.toAddressHashMapList(addresses));
157+
} catch (IOException e) {
158+
result.error(
159+
"IO_ERROR",
160+
String.format("A network error occurred trying to lookup the address '%s'.", address),
161+
null
162+
);
163+
}
164+
}
165+
119166
private void onPlacemarkFromCoordinates(final MethodCall call, final Result result) {
120167
final double latitude = call.argument("latitude");
121168
final double longitude = call.argument("longitude");
122-
final String languageTag = call.argument("localeIdentifier");
123169

124170
try {
125171
final List<Address> addresses = geocoding.placemarkFromCoordinates(
126172
latitude,
127-
longitude,
128-
LocaleConverter.fromLanguageTag(languageTag));
173+
longitude);
174+
129175
if (addresses == null || addresses.isEmpty()) {
130176
result.error(
131177
"NOT_FOUND",
132-
String.format("No address information found for supplied coordinates (latitude: %f, longitude: %f).", latitude, longitude),
178+
String.format(
179+
Locale.ENGLISH,
180+
"No address information found for supplied coordinates (latitude: %f, longitude: %f).",
181+
latitude,
182+
longitude
183+
),
133184
null);
134185
return;
135186
}
136187
result.success(AddressMapper.toAddressHashMapList(addresses));
137188
} catch (IOException ex) {
138189
result.error(
139190
"IO_ERROR",
140-
String.format("A network error occurred trying to lookup the supplied coordinates (latitude: %f, longitude: %f).", latitude, longitude),
191+
String.format(
192+
Locale.ENGLISH,
193+
"A network error occurred trying to lookup the supplied coordinates (latitude: %f, longitude: %f).",
194+
latitude,
195+
longitude
196+
),
141197
null
142198
);
143199
}

geocoding_android/example/lib/plugin_example/geocode_page.dart

+33
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,39 @@ class _GeocodeWidgetState extends State<GeocodeWidget> {
103103
const Padding(
104104
padding: EdgeInsets.only(top: 8),
105105
),
106+
Center(
107+
child: ElevatedButton(
108+
child: Text('Look up address'),
109+
onPressed: () {
110+
GeocodingAndroid()
111+
.placemarkFromAddress(_addressController.text)
112+
.then((locations) {
113+
var output = 'No results found.';
114+
if (locations.isNotEmpty) {
115+
output = locations[0].toString();
116+
}
117+
118+
setState(() {
119+
_output = output;
120+
});
121+
});
122+
}),
123+
),
124+
const Padding(
125+
padding: EdgeInsets.only(top: 32),
126+
),
127+
TextField(
128+
autocorrect: false,
129+
controller: _addressController,
130+
style: Theme.of(context).textTheme.bodyMedium,
131+
decoration: InputDecoration(
132+
hintText: 'Address',
133+
),
134+
keyboardType: TextInputType.text,
135+
),
136+
const Padding(
137+
padding: EdgeInsets.only(top: 8),
138+
),
106139
Center(
107140
child: ElevatedButton(
108141
child: Text('Look up location'),

geocoding_android/example/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dependencies:
1919
# The example app is bundled with the plugin so we use a path dependency on
2020
# the parent directory to use the current plugin's version.
2121
path: ../
22-
geocoding_platform_interface: ^2.0.0
22+
geocoding_platform_interface: ^3.1.0
2323

2424

2525
# The following adds the Cupertino Icons font to your application.

geocoding_android/lib/geocoding_android.dart

+36-13
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ class GeocodingAndroid extends GeocodingPlatform {
1212
GeocodingPlatform.instance = GeocodingAndroid();
1313
}
1414

15+
@override
16+
Future<void> setLocaleIdentifier(
17+
String localeIdentifier,
18+
) {
19+
final parameters = <String, String>{
20+
'localeIdentifier': localeIdentifier,
21+
};
22+
23+
return _channel.invokeMethod('setLocaleIdentifier', parameters);
24+
}
25+
1526
@override
1627
Future<List<Location>> locationFromAddress(
17-
String address, {
18-
String? localeIdentifier,
19-
}) async {
28+
String address,
29+
) async {
2030
final parameters = <String, String>{
2131
'address': address,
2232
};
2333

24-
if (localeIdentifier != null) {
25-
parameters['localeIdentifier'] = localeIdentifier;
26-
}
2734
try {
2835
final placemarks = await _channel.invokeMethod(
2936
'locationFromAddress',
@@ -40,23 +47,39 @@ class GeocodingAndroid extends GeocodingPlatform {
4047
@override
4148
Future<List<Placemark>> placemarkFromCoordinates(
4249
double latitude,
43-
double longitude, {
44-
String? localeIdentifier,
45-
}) async {
50+
double longitude,
51+
) async {
4652
final parameters = <String, dynamic>{
4753
'latitude': latitude,
4854
'longitude': longitude,
4955
};
5056

51-
if (localeIdentifier != null) {
52-
parameters['localeIdentifier'] = localeIdentifier;
53-
}
54-
5557
final placemarks =
5658
await _channel.invokeMethod('placemarkFromCoordinates', parameters);
5759
return Placemark.fromMaps(placemarks);
5860
}
5961

62+
@override
63+
Future<List<Placemark>> placemarkFromAddress(
64+
String address,
65+
) async {
66+
final parameters = <String, String>{
67+
'address': address,
68+
};
69+
70+
try {
71+
final placemarks = await _channel.invokeMethod(
72+
'placemarkFromAddress',
73+
parameters,
74+
);
75+
76+
return Placemark.fromMaps(placemarks);
77+
} on PlatformException catch (e) {
78+
_handlePlatformException(e);
79+
rethrow;
80+
}
81+
}
82+
6083
void _handlePlatformException(PlatformException platformException) {
6184
switch (platformException.code) {
6285
case 'NOT_FOUND':

geocoding_android/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: geocoding_android
22
description: A Flutter Geocoding plugin which provides easy geocoding and reverse-geocoding features.
3-
version: 2.1.2
3+
version: 3.0.0
44
repository: https://github.com/baseflow/flutter-geocoding/tree/main/geocoding_android
55
issue_tracker: https://github.com/Baseflow/flutter-geocoding/issues
66

@@ -12,7 +12,7 @@ dependencies:
1212
flutter:
1313
sdk: flutter
1414

15-
geocoding_platform_interface: ^2.0.0
15+
geocoding_platform_interface: ^3.1.0
1616

1717
dev_dependencies:
1818
flutter_test:

geocoding_android/test/geocoding_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final mockLocation = Location(
99
timestamp: DateTime.fromMillisecondsSinceEpoch(0).toUtc(),
1010
);
1111

12-
final mockPlacemark = Placemark(
12+
const mockPlacemark = Placemark(
1313
administrativeArea: 'Overijssel',
1414
country: 'Netherlands',
1515
isoCountryCode: 'NL',

0 commit comments

Comments
 (0)