0

I am new to Android Studio and am trying to retrieve the co-ordinates of an Android device using Location Service.

I have all the relevant permissions enabled in the manifest (the Coarse and Fine locations, and the Internet).

In my MainActivity.java, I have the following. However, when I run it in Android Studio, for the 3 test cases I have in the activity, the TextView ShowLocation always become "Something else", meaning that it went to the third case where location is null (no location detected?) but permission has been granted properly.

What am I doing wrong here? Is it because I could not test Location Service in Android Studio? How can I test and obtain co-ordinate values in an Android application using Android Studio?

A snippet of MainActivity.java

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
                }

                Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                if (loc != null) {
                    latitude = String.valueOf(loc.getLatitude());
                    showLocation.setText("Latitude" + latitude);
                }

                else {

                    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED) {
                        showLocation.setText("No GPS Permission granted");
                   
                    } else
                        showLocation.setText("Something else");
                }
NicTam
  • 41
  • 10
  • Have your declared ACCESS_FINE_LOCATION in manifest.xml file ? – NRUSINGHA MOHARANA Apr 14 '21 at 04:27
  • @NRUSINGHAMOHARANA Yup, that has been declared in the manifest file. – NicTam Apr 14 '21 at 04:29
  • did you try with `Location loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);` but you need to add network permission in the manifest – Shay Kin Apr 14 '21 at 04:37
  • 1
    please visit https://stackoverflow.com/questions/17519198/how-to-get-the-current-location-latitude-and-longitude-in-android – NRUSINGHA MOHARANA Apr 14 '21 at 05:14
  • The `getLastKnownLocation()` just returns the last known location (hence the name) if one exists. It may also return `null`. It doesn't power up the GPS module and fetch a fresh up-to-date location. For that you need to request location updates. And GPS won't work indoors. – Markus Kauppinen Apr 14 '21 at 09:59
  • @ShayKin I have tried `NETWORK_PROVIDER` as well, still seemed to have the same problem. @MarkusKauppinen, any insight on how to power up to GPS module? – NicTam Apr 14 '21 at 11:21
  • [https://developer.android.com/training/location/request-updates](https://developer.android.com/training/location/request-updates) – Markus Kauppinen Apr 16 '21 at 07:09

0 Answers0