diff options
author | Maxime Biais <maxime.biais@gmail.com> | 2014-07-29 20:38:37 +0200 |
---|---|---|
committer | Maxime Biais <maxime.biais@gmail.com> | 2014-07-29 20:38:37 +0200 |
commit | 36eb74d46a534809350fba165d3decbfbb6020ab (patch) | |
tree | 5ea210fb3a06d4598f603d7925cf3a4fa3976ddb /libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/GeocoderUtils.java | |
parent | dff352bc7d1b9ee8f75a88eac97736fa9d397b8f (diff) | |
parent | 449639d78f9bc643d34a794dacf361f2b31c007e (diff) | |
download | gradle-perf-android-medium-36eb74d46a534809350fba165d3decbfbb6020ab.tar.gz |
Add 'libs/utils/' from commit '449639d78f9bc643d34a794dacf361f2b31c007e'
git-subtree-dir: libs/utils
git-subtree-mainline: dff352bc7d1b9ee8f75a88eac97736fa9d397b8f
git-subtree-split: 449639d78f9bc643d34a794dacf361f2b31c007e
Diffstat (limited to 'libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/GeocoderUtils.java')
-rw-r--r-- | libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/GeocoderUtils.java | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/GeocoderUtils.java b/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/GeocoderUtils.java new file mode 100644 index 000000000..e861a88b8 --- /dev/null +++ b/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/GeocoderUtils.java @@ -0,0 +1,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, Locale.getDefault()); + } 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; + } +} |