0

i was building an app where i need to get the device location and heres the code i used

Double latitude = 0.0;
    Double longitude = 0.0;
    String TAG = "HomeActivity";
    Location gpsLoc = null, networkLoc = null, finallLoc = null;
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this,"Not Granted",Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(this,"Granted",Toast.LENGTH_LONG).show();
            }
            try{
                gpsLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                networkLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            }catch(Exception e){
                e.printStackTrace();
            }
            if(gpsLoc!= null){
                finallLoc = gpsLoc;
                latitude = finallLoc.getLatitude();
                longitude = finallLoc.getLongitude();
            }else if(networkLoc!=null){
                finallLoc = networkLoc;
                latitude = finallLoc.getLatitude();
                longitude = finallLoc.getLongitude();
            }else{
                latitude = 0.0;
                longitude=0.0;
            }
            try{
                Geocoder geocoder = new Geocoder(getApplicationContext(),Locale.getDefault());
                List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1);
                if(addresses!=null && addresses.size()>0){
                    String country = addresses.get(0).getCountryName();
                    String address = addresses.get(0).getAddressLine(0);
                    String city = addresses.get(0).getLocality();

                }

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

i am almost ready to deploy this app and i wanted to make sure if i can rely on this code to get the location correctly?if no what kind of improvements should i make? the code works fine just wanted to know if there's something i am missing or something that could lead to an error under certain conditions.

Jhon
  • 99
  • 3
  • 15

1 Answers1

0

You can get the last known location from gps/network with the above code. But not sure that you will get the current location or sometimes you may not get the location.

If the location is mandatory for you then you should force the user to turn on gps and get the current location. This can be achieved by using LocationManager.requestLocationUpdates

Please follow the link https://stackoverflow.com/a/10917500/3886504

shobhan
  • 1,460
  • 2
  • 14
  • 28