Home > other >  GeoCoder Does not show contryName
GeoCoder Does not show contryName

Time:07-18

I am making an android application.In my application I want to get the Country name of my location using my LocationTracker Class I created , but I cant get the Country name using GeoCoder

Here is my LocationTracker Class

package com.farzin.locationmap.Location;


import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.farzin.locationmap.R;

import java.util.List;

public class LocationTracker extends Service implements LocationListener {

    Location location;
    LocationManager locationManager;
    Context context;

    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = true;
    boolean canGetLocation;

    double longitude;
    double latitude;

    static final long MIN_DISTANCE = 10;
    static final long MIN_TIME = 1000 * 60;

    public LocationTracker(Context context) {
        this.context = context;
        getLocation();
    }


    public Location getLocation(){

        locationManager = (LocationManager)context.getSystemService(LOCATION_SERVICE);

        //gps check
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        //network check
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isNetworkEnabled && !isGPSEnabled){
            canGetLocation = false;
        }else {
            canGetLocation = true;



            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    MIN_TIME,MIN_DISTANCE,this);

            if (locationManager != null){

                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                if (location != null){
                    longitude = location.getLongitude();
                    latitude = location.getLatitude();
                }
            }

            if (isGPSEnabled){

                if (location == null){
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                            MIN_TIME,MIN_DISTANCE,this);

                    if (locationManager != null){
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if (location != null){
                            longitude = location.getLongitude();
                            latitude  = location.getLatitude();
                        }
                    }
                }
            }
        }
        return location;
    }

    public double getLongitude() {
        return longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public boolean hasLocation(){
        return canGetLocation;
    }

    public void showAlertSettings(){

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

        alertDialog.setTitle(R.string.warning);

        alertDialog.setMessage(R.string.gps_warning_massage);

        alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });

        alertDialog.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
    }

Here is my Main Activity Where im trying to create for Toast the Country Name For testing

locationTracker = new LocationTracker(getApplicationContext());

        if (locationTracker.hasLocation()){

            lat = locationTracker.getLatitude();
            lon = locationTracker.getLongitude();

            Geocoder geocoder = new Geocoder(getApplicationContext(),new Locale("fa"));
            try {
                List<Address> addressList = geocoder.getFromLocation(lat, lon, 1);

                Address address = addressList.get(0);
                String countryName = address.getCountryName();
                Toast.makeText(this, countryName, Toast.LENGTH_SHORT).show();

             } catch (IOException e) {
                e.printStackTrace();
            }catch (Exception e){
                e.printStackTrace();
            }



        }else {
            locationTracker.showAlertSettings();
        }


I'm new to this, so what seems to be the problem here?

plus ignore the part where I didnt ask for permissions from the user this is only for testing purposes...

CodePudding user response:

There are two cases that came into my mind.

First case:

Geocoder does not reteieve any data if the device is in a region that Google denies requests coming from it. (such as Crimea, Cuba, the so-called Donetsk People's Republic and Luhansk People's Republic, Iran, North Korea, and Syria.)

You may want to try using a VPN that would change your IP, and try to see if you get data in the response.

Second case:

You may want to try different location and see if there is data retrived in the response. If another location did retreive data, then that's most probably could mean that Geocoder actually did not get any data from the previously provided location.

CodePudding user response:

Try this

    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(requireActivity(), Locale.getDefault());

    try {
       addresses = geocoder.getFromLocation(lat, lng, 1);
       String country = addresses.get(0).getCountryName();

    }catch (IOException e)
    {
      e.printStackTrace();
    }
  • Related