3
3
import android .location .Address ;
4
4
import android .util .Log ;
5
5
6
+ import androidx .annotation .NonNull ;
6
7
import androidx .annotation .Nullable ;
7
8
8
9
import com .baseflow .geocoding .utils .AddressMapper ;
9
10
import com .baseflow .geocoding .utils .LocaleConverter ;
10
11
11
12
import java .io .IOException ;
12
13
import java .util .List ;
14
+ import java .util .Locale ;
13
15
14
16
import io .flutter .plugin .common .BinaryMessenger ;
15
17
import io .flutter .plugin .common .MethodCall ;
@@ -36,11 +38,20 @@ final class MethodCallHandlerImpl implements MethodCallHandler {
36
38
}
37
39
38
40
@ 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
+ ) {
40
45
switch (call .method ) {
46
+ case "setLocaleIdentifier" :
47
+ setLocaleIdentifier (call , result );
48
+ break ;
41
49
case "locationFromAddress" :
42
50
onLocationFromAddress (call , result );
43
51
break ;
52
+ case "placemarkFromAddress" :
53
+ onPlacemarkFromAddress (call , result );
54
+ break ;
44
55
case "placemarkFromCoordinates" :
45
56
onPlacemarkFromCoordinates (call , result );
46
57
break ;
@@ -82,9 +93,16 @@ void stopListening() {
82
93
channel = null ;
83
94
}
84
95
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
+
85
104
private void onLocationFromAddress (MethodCall call , Result result ) {
86
105
final String address = call .argument ("address" );
87
- final String languageTag = call .argument ("localeIdentifier" );
88
106
89
107
if (address == null || address .isEmpty ()) {
90
108
result .error (
@@ -94,9 +112,7 @@ private void onLocationFromAddress(MethodCall call, Result result) {
94
112
}
95
113
96
114
try {
97
- final List <Address > addresses = geocoding .placemarkFromAddress (
98
- address ,
99
- LocaleConverter .fromLanguageTag (languageTag ));
115
+ final List <Address > addresses = geocoding .placemarkFromAddress (address );
100
116
101
117
if (addresses == null || addresses .isEmpty ()) {
102
118
result .error (
@@ -110,34 +126,74 @@ private void onLocationFromAddress(MethodCall call, Result result) {
110
126
} catch (IOException ex ) {
111
127
result .error (
112
128
"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 ),
114
130
null
115
131
);
116
132
}
117
133
}
118
134
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
+
119
166
private void onPlacemarkFromCoordinates (final MethodCall call , final Result result ) {
120
167
final double latitude = call .argument ("latitude" );
121
168
final double longitude = call .argument ("longitude" );
122
- final String languageTag = call .argument ("localeIdentifier" );
123
169
124
170
try {
125
171
final List <Address > addresses = geocoding .placemarkFromCoordinates (
126
172
latitude ,
127
- longitude ,
128
- LocaleConverter . fromLanguageTag ( languageTag ));
173
+ longitude );
174
+
129
175
if (addresses == null || addresses .isEmpty ()) {
130
176
result .error (
131
177
"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
+ ),
133
184
null );
134
185
return ;
135
186
}
136
187
result .success (AddressMapper .toAddressHashMapList (addresses ));
137
188
} catch (IOException ex ) {
138
189
result .error (
139
190
"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
+ ),
141
197
null
142
198
);
143
199
}
0 commit comments