Search in sources :

Example 6 with Geocoder

use of android.location.Geocoder 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 7 with Geocoder

use of android.location.Geocoder in project YourAppIdea by Michenux.

the class AroundMeFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ((YourApplication) getActivity().getApplication()).inject(this);
    setRetainInstance(true);
    setHasOptionsMenu(true);
    mGoogleApiClient = new GoogleApiClient.Builder(this.getActivity()).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
    mRequest = LocationRequest.create().setInterval(15000).setFastestInterval(5000).setSmallestDisplacement(50).setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    mGeocoder = new Geocoder(this.getActivity(), Locale.getDefault());
    mPlaceListAdapter = new PlaceRecyclerAdapter(new ArrayList<Place>());
    if (savedInstanceState != null) {
        this.mUseLocationClient = savedInstanceState.getBoolean("useLocationClient", true);
        this.mCityName = savedInstanceState.getString("cityName");
        this.mCurrentLocation = savedInstanceState.getParcelable("currentLocation");
        this.mLocalProvider = savedInstanceState.getBoolean("localProvider", true);
    }
    if (this.mLocalProvider) {
        mPlaceProvider = new PlaceLocalProvider(this, this);
    } else {
        mPlaceProvider = new PlaceRemoteProvider(this, this);
    }
}
Also used : YourApplication(org.michenux.yourappidea.YourApplication) GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) ArrayList(java.util.ArrayList) Geocoder(android.location.Geocoder)

Example 8 with Geocoder

use of android.location.Geocoder 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 9 with Geocoder

use of android.location.Geocoder 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)

Example 10 with Geocoder

use of android.location.Geocoder in project Remindy by abicelis.

the class FetchAddressIntentService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    String errorMessage = "";
    // Get the location passed to this service through an extra.
    Location location = intent.getParcelableExtra(LOCATION_DATA_EXTRA);
    mReceiver = intent.getParcelableExtra(RECEIVER);
    List<Address> addresses = null;
    try {
        addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
    } catch (IOException ioException) {
        // Catch network or other I/O problems.
        errorMessage = getString(R.string.service_not_available);
        Log.e(TAG, errorMessage, ioException);
    } catch (IllegalArgumentException illegalArgumentException) {
        // Catch invalid latitude or longitude values.
        errorMessage = getString(R.string.invalid_lat_long_used);
        Log.e(TAG, errorMessage + ". " + "Latitude = " + location.getLatitude() + ", Longitude = " + location.getLongitude(), illegalArgumentException);
    }
    // Handle case where no address was found.
    if (addresses == null || addresses.size() == 0) {
        if (errorMessage.isEmpty()) {
            errorMessage = getString(R.string.no_address_found);
            Log.e(TAG, errorMessage);
        }
        deliverResultToReceiver(FAILURE_RESULT, errorMessage, "");
    } else {
        Address address = addresses.get(0);
        ArrayList<String> addressFragments = new ArrayList<String>();
        // join them, and send them to the thread.
        for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
            addressFragments.add(address.getAddressLine(i));
        }
        Log.i(TAG, getString(R.string.address_found));
        //deliverResultToReceiver(SUCCESS_RESULT, address.getFeatureName(), TextUtils.join(System.getProperty("line.separator"), addressFragments) );
        deliverResultToReceiver(SUCCESS_RESULT, address.getFeatureName(), TextUtils.join(", ", addressFragments));
    }
}
Also used : Address(android.location.Address) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Geocoder(android.location.Geocoder) Location(android.location.Location)

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