Search in sources :

Example 6 with Address

use of android.location.Address in project WordPress-Android by wordpress-mobile.

the class GeocoderUtils method getAddressFromLocationName.

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;
}
Also used : Address(android.location.Address) IOException(java.io.IOException) Geocoder(android.location.Geocoder)

Example 7 with Address

use of android.location.Address in project android_frameworks_base by DirtyUnicorns.

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 8 with Address

use of android.location.Address in project android_frameworks_base by DirtyUnicorns.

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)

Example 9 with Address

use of android.location.Address in project android_frameworks_base by AOSPA.

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)

Example 10 with Address

use of android.location.Address in project Space-Station-Tracker by Kiarasht.

the class Locations method Connected.

/**
     * Lets find user's location.
     */
private void Connected() {
    // Check if we have the right permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, R.string.errorPermissionLocation, Toast.LENGTH_LONG).show();
        return;
    }
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);
    Location location = null;
    for (int i = providers.size() - 1; i >= 0; i--) {
        location = locationManager.getLastKnownLocation(providers.get(i));
        if (location != null)
            break;
    }
    // If we got something back start parsing
    if (location != null) {
        String url = "https://maps.googleapis.com/maps/api/staticmap?" + "center=LAT,LNG&" + "zoom=13&" + "scale=1&" + "size=640x640&" + "markers=color:red%7CLAT,LNG&" + "key=AIzaSyAtpWPhzhbtqTgofnQhAHjiG12MmrY2AAE";
        mLatitude = String.valueOf(location.getLatitude());
        mLongitude = String.valueOf(location.getLongitude());
        url = url.replace("LAT", mLatitude);
        url = url.replace("LNG", mLongitude);
        try {
            List<Address> matches = new Geocoder(this).getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            final Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
            if (bestMatch != null) {
                String locationFormat = "";
                if (!"null".equals(bestMatch.getLocality())) {
                    locationFormat += bestMatch.getLocality() + ", ";
                }
                if (!"null".equals(bestMatch.getAdminArea())) {
                    locationFormat += bestMatch.getAdminArea() + " ";
                }
                if (!"null".equals(bestMatch.getCountryCode())) {
                    locationFormat += bestMatch.getCountryCode() + " ";
                }
                if (!"null".equals(bestMatch.getPostalCode())) {
                    locationFormat += bestMatch.getPostalCode();
                }
                mTitleView.setText(locationFormat);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Picasso.with(mActivity).load(url).into(mImageView);
        displayResults();
        displayPasses(null, null, null);
    } else {
        Toast.makeText(this, R.string.errorLocation, Toast.LENGTH_LONG).show();
    }
}
Also used : LocationManager(android.location.LocationManager) Address(android.location.Address) IOException(java.io.IOException) Geocoder(android.location.Geocoder) Location(android.location.Location)

Aggregations

Address (android.location.Address)34 Geocoder (android.location.Geocoder)22 IOException (java.io.IOException)18 Locale (java.util.Locale)6 Location (android.location.Location)4 ArrayList (java.util.ArrayList)4 List (java.util.List)3 SuppressLint (android.annotation.SuppressLint)1 ContentValues (android.content.ContentValues)1 Intent (android.content.Intent)1 TypedArray (android.content.res.TypedArray)1 Cursor (android.database.Cursor)1 Point (android.graphics.Point)1 LocationManager (android.location.LocationManager)1 ExifInterface (android.support.media.ExifInterface)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ImageButton (android.widget.ImageButton)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1