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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
package org.wordpress.android.util;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public final class GeocoderUtils {
private GeocoderUtils() {
throw new AssertionError();
}
public static Geocoder getGeocoder(Context context) {
// first make sure a Geocoder service exists on this device (requires API 9)
if (!Geocoder.isPresent()) {
return null;
}
Geocoder gcd;
try {
gcd = new Geocoder(context, LanguageUtils.getCurrentDeviceLanguage(context));
} catch (NullPointerException cannotIstantiateEx) {
AppLog.e(AppLog.T.UTILS, "Cannot instantiate Geocoder", cannotIstantiateEx);
return null;
}
return gcd;
}
public static Address getAddressFromCoords(Context context, double latitude, double longitude) {
Address address = null;
List<Address> addresses = null;
Geocoder gcd = getGeocoder(context);
if (gcd == null) {
return null;
}
try {
addresses = gcd.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
// may get "Unable to parse response from server" IOException here if Geocoder
// service is hit too frequently
AppLog.e(AppLog.T.UTILS,
"Unable to parse response from server. Is Geocoder service hitting the server too frequently?",
e
);
}
// addresses may be null or empty if network isn't connected
if (addresses != null && addresses.size() > 0) {
address = addresses.get(0);
}
return address;
}
public static Address getAddressFromLocationName(Context context, String locationName) {
int maxResults = 1;
Address address = null;
List<Address> addresses = null;
Geocoder gcd = getGeocoder(context);
if (gcd == null) {
return null;
}
try {
addresses = gcd.getFromLocationName(locationName, maxResults);
} catch (IOException e) {
AppLog.e(AppLog.T.UTILS, "Failed to get coordinates from location", e);
}
// addresses may be null or empty if network isn't connected
if (addresses != null && addresses.size() > 0) {
address = addresses.get(0);
}
return address;
}
public static String getLocationNameFromAddress(Address address) {
String locality = "", adminArea = "", country = "";
if (address.getLocality() != null) {
locality = address.getLocality();
}
if (address.getAdminArea() != null) {
adminArea = address.getAdminArea();
}
if (address.getCountryName() != null) {
country = address.getCountryName();
}
return ((locality.equals("")) ? locality : locality + ", ")
+ ((adminArea.equals("")) ? adminArea : adminArea + " ") + country;
}
public static double[] getCoordsFromAddress(Address address) {
double[] coordinates = new double[2];
if (address.hasLatitude() && address.hasLongitude()) {
coordinates[0] = address.getLatitude();
coordinates[1] = address.getLongitude();
}
return coordinates;
}
}
|