4
4
import android .content .Context ;
5
5
import android .location .Address ;
6
6
import android .location .Geocoder ;
7
-
7
+ import android .os .Build ;
8
+ import androidx .annotation .RequiresApi ;
8
9
import java .io .IOException ;
10
+ import java .util .ArrayList ;
9
11
import java .util .List ;
10
12
import java .util .Locale ;
11
13
@@ -34,10 +36,27 @@ void setLocaleIdentifier(@Nullable Locale locale) {
34
36
* @return a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
35
37
* @throws java.io.IOException if the network is unavailable or any other I/O problem occurs.
36
38
*/
39
+ @ SuppressWarnings ("deprecation" )
37
40
List <Address > placemarkFromAddress (String address ) throws IOException {
38
-
39
41
final Geocoder geocoder = createGeocoder (androidContext , locale );
40
- return geocoder .getFromLocationName (address , 5 );
42
+
43
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
44
+ return getAddressesWithGeocodeListener (geocoder , address , 5 );
45
+ } else {
46
+ return geocoder .getFromLocationName (address , 5 );
47
+ }
48
+ }
49
+
50
+ @ RequiresApi (api = Build .VERSION_CODES .TIRAMISU )
51
+ private List <Address > getAddressesWithGeocodeListener (Geocoder geocoder , String address , int maxResults ) {
52
+ List <Address > addresses = new ArrayList <>();
53
+ geocoder .getFromLocationName (address , maxResults , new Geocoder .GeocodeListener () {
54
+ @ Override
55
+ public void onGeocode (List <Address > geocodedAddresses ) {
56
+ addresses .addAll (geocodedAddresses );
57
+ }
58
+ });
59
+ return addresses ;
41
60
}
42
61
43
62
/**
@@ -48,12 +67,29 @@ List<Address> placemarkFromAddress(String address) throws IOException {
48
67
* @return a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
49
68
* @throws IOException if the network is unavailable or any other I/O problem occurs.
50
69
*/
70
+ @ SuppressWarnings ("deprecation" )
51
71
List <Address > placemarkFromCoordinates (
52
72
double latitude ,
53
73
double longitude
54
74
) throws IOException {
55
75
final Geocoder geocoder = createGeocoder (androidContext , locale );
56
- return geocoder .getFromLocation (latitude , longitude , 5 );
76
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
77
+ return getLocationWithGeocodeListener (geocoder , latitude , longitude , 5 );
78
+ } else {
79
+ return geocoder .getFromLocation (latitude , longitude , 5 );
80
+ }
81
+ }
82
+
83
+ @ RequiresApi (api = Build .VERSION_CODES .TIRAMISU )
84
+ private List <Address > getLocationWithGeocodeListener (Geocoder geocoder , double latitude , double longitude , int maxResults ) {
85
+ List <Address > addresses = new ArrayList <>();
86
+ geocoder .getFromLocation (latitude , longitude , maxResults , new Geocoder .GeocodeListener () {
87
+ @ Override
88
+ public void onGeocode (List <Address > geocodedAddresses ) {
89
+ addresses .addAll (geocodedAddresses );
90
+ }
91
+ });
92
+ return addresses ;
57
93
}
58
94
59
95
private static Geocoder createGeocoder (Context androidContext , @ Nullable Locale locale ) {
0 commit comments