Search in sources :

Example 11 with Geocoder

use of android.location.Geocoder in project Ushahidi_Android by ushahidi.

the class GeocoderTask method doInBackground.

@Override
protected String doInBackground(Double... location) {
    Log.i(getClass().getSimpleName(), String.format("doInBackground %s", Arrays.toString(location)));
    Geocoder geoCoder = new Geocoder(context);
    try {
        List<Address> addresses = geoCoder.getFromLocation(location[0], location[1], 1);
        StringBuilder addressString = new StringBuilder();
        if (addresses != null && addresses.size() > 0) {
            Address address = addresses.get(0);
            if (address.getFeatureName() != null) {
                addressString.append(address.getFeatureName());
            }
            if (address.getThoroughfare() != null) {
                if (addressString.length() > 0) {
                    addressString.append(", ");
                }
                addressString.append(address.getThoroughfare());
            }
            if (address.getSubAdminArea() != null) {
                if (addressString.length() > 0) {
                    addressString.append(", ");
                }
                addressString.append(address.getSubAdminArea());
            }
            if (address.getLocality() != null && !address.getLocality().equalsIgnoreCase(address.getSubAdminArea())) {
                if (addressString.length() > 0) {
                    addressString.append(", ");
                }
                addressString.append(address.getLocality());
            }
            if (address.getCountryName() != null) {
                if (addressString.length() > 0) {
                    addressString.append(", ");
                }
                addressString.append(address.getCountryName());
            }
        }
        return addressString.toString();
    } catch (IOException ioe) {
        Log.e(getClass().getSimpleName(), "IOException", ioe);
    } catch (IllegalArgumentException ioe) {
        Log.e(getClass().getSimpleName(), "IllegalArgumentException", ioe);
    }
    return null;
}
Also used : Address(android.location.Address) IOException(java.io.IOException) Geocoder(android.location.Geocoder)

Example 12 with Geocoder

use of android.location.Geocoder in project android_frameworks_base by crdroidandroid.

the class GeocoderTest method testGeocoder.

public void testGeocoder() throws Exception {
    Locale locale = new Locale("en", "us");
    Geocoder g = new Geocoder(mContext, locale);
    List<Address> addresses1 = g.getFromLocation(37.435067, -122.166767, 2);
    assertNotNull(addresses1);
    assertEquals(1, addresses1.size());
    Address addr = addresses1.get(0);
    assertEquals("94305", addr.getFeatureName());
    assertEquals("Palo Alto, CA 94305", addr.getAddressLine(0));
    assertEquals("USA", addr.getAddressLine(1));
    assertEquals("94305", addr.getPostalCode());
    assertFalse(Math.abs(addr.getLatitude() - 37.4240385) > 0.1);
    List<Address> addresses2 = g.getFromLocationName("San Francisco, CA", 1);
    assertNotNull(addresses2);
    assertEquals(1, addresses2.size());
    addr = addresses2.get(0);
    assertEquals("San Francisco", addr.getFeatureName());
    assertEquals("San Francisco, CA", addr.getAddressLine(0));
    assertEquals("United States", addr.getAddressLine(1));
    assertEquals("San Francisco", addr.getLocality());
    assertEquals("CA", addr.getAdminArea());
    assertEquals(null, addr.getPostalCode());
    assertFalse(Math.abs(addr.getLatitude() - 37.77916) > 0.1);
}
Also used : Locale(java.util.Locale) Address(android.location.Address) Geocoder(android.location.Geocoder)

Example 13 with Geocoder

use of android.location.Geocoder in project Android-ReactiveLocation by mcharmas.

the class GeocodeObservable method call.

@Override
public void call(Subscriber<? super List<Address>> subscriber) {
    Geocoder geocoder = new Geocoder(ctx);
    List<Address> result;
    try {
        if (bounds != null) {
            result = geocoder.getFromLocationName(locationName, maxResults, bounds.southwest.latitude, bounds.southwest.longitude, bounds.northeast.latitude, bounds.northeast.longitude);
        } else {
            result = geocoder.getFromLocationName(locationName, maxResults);
        }
        if (!subscriber.isUnsubscribed()) {
            subscriber.onNext(result);
            subscriber.onCompleted();
        }
    } catch (IOException e) {
        if (!subscriber.isUnsubscribed()) {
            subscriber.onError(e);
        }
    }
}
Also used : Address(android.location.Address) IOException(java.io.IOException) Geocoder(android.location.Geocoder)

Example 14 with Geocoder

use of android.location.Geocoder in project Android-ReactiveLocation by mcharmas.

the class ReverseGeocodeObservable method call.

@Override
public void call(final Subscriber<? super List<Address>> subscriber) {
    Geocoder geocoder = new Geocoder(ctx, locale);
    try {
        subscriber.onNext(geocoder.getFromLocation(latitude, longitude, maxResults));
        subscriber.onCompleted();
    } catch (IOException e) {
        // If it's a service not available error try a different approach using google web api
        if (e.getMessage().equalsIgnoreCase("Service not Available")) {
            Observable.create(new FallbackReverseGeocodeObservable(locale, latitude, longitude, maxResults)).subscribeOn(Schedulers.io()).subscribe(subscriber);
        } else {
            subscriber.onError(e);
        }
    }
}
Also used : IOException(java.io.IOException) Geocoder(android.location.Geocoder)

Example 15 with Geocoder

use of android.location.Geocoder in project platform_frameworks_base by android.

the class LocationBasedCountryDetector method getCountryFromLocation.

/**
     * @return the ISO 3166-1 two letters country code from the location
     */
protected String getCountryFromLocation(Location location) {
    String country = null;
    Geocoder geoCoder = new Geocoder(mContext);
    try {
        List<Address> addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if (addresses != null && addresses.size() > 0) {
            country = addresses.get(0).getCountryCode();
        }
    } catch (IOException e) {
        Slog.w(TAG, "Exception occurs when getting country from location");
    }
    return country;
}
Also used : Address(android.location.Address) IOException(java.io.IOException) Geocoder(android.location.Geocoder)

Aggregations

Geocoder (android.location.Geocoder)27 Address (android.location.Address)22 IOException (java.io.IOException)16 Locale (java.util.Locale)6 ArrayList (java.util.ArrayList)4 Location (android.location.Location)3 Handler (android.os.Handler)2 View (android.view.View)2 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)2 List (java.util.List)2 SuppressLint (android.annotation.SuppressLint)1 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 TypedArray (android.content.res.TypedArray)1 ContentObserver (android.database.ContentObserver)1 Point (android.graphics.Point)1 LocationManager (android.location.LocationManager)1 Uri (android.net.Uri)1 HandlerThread (android.os.HandlerThread)1 SearchView (android.support.v7.widget.SearchView)1