blob: 1582daabc901fc1ab132a98d9a999d5612861f77 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package cgeo.geocaching.location;
import cgeo.geocaching.utils.Log;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import android.content.Context;
import android.location.Address;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
/**
* Encapsulation of the Android {@link android.location.Geocoder} with default error handling. All methods of this class
* are blocking and will do network lookups.
*
*/
public class Geocoder {
private final android.location.Geocoder geocoder;
public Geocoder(final Context context) {
geocoder = new android.location.Geocoder(context, Locale.getDefault());
}
/**
* @param keyword
* @return
*
* @see android.location.Geocoder#getFromLocationName(String, int)
*/
public @NonNull List<Address> getFromLocationName(final String keyword) {
try {
return geocoder.getFromLocationName(keyword, 20);
} catch (final Exception e) {
handleException(e);
return Collections.emptyList();
}
}
public @NonNull List<Address> getFromLocation(final Geopoint coords) {
try {
return geocoder.getFromLocation(coords.getLatitude(), coords.getLongitude(), 20);
} catch (final IOException e) {
handleException(e);
return Collections.emptyList();
}
}
private static void handleException(final Exception e) {
// non Google devices come without the geocoder
if (StringUtils.containsIgnoreCase(e.getMessage(), "Service not Available")) {
Log.i("No geocoder available");
}
else {
Log.e("Geocoder", e);
}
}
}
|