2

I'm simply trying to write a method that returns Latitude and Longitude. I have an activity with a button and two text fields. The java class I am using extends AppCompatActivity and implements LocationListener When the button is pressed the following method is pressed:

public void startGPS(View view)
{

        // Here is the code to handle permissions - you should not need to edit this.
        if ( Build.VERSION.SDK_INT >= 23 &&
                ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION }, TAKE_PHOTO_PERMISSION);
        }

        Location location = locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, this);
}

Later on I try to print out my location in my onLocationChanged method but it was causing the app to crash. I ran it through the debugger and found that the location was always null.

I tried to this solution but it didn't work. Other examples are calling the function in onResume but I really need this to be in startGPS method.

Also, is there a chance that the error is just with my device? I'm running it on a Nexus 7 which doesn't seem to have any problems when I run Google Maps.

Community
  • 1
  • 1

3 Answers3

1

If are trying to return the GPS Coordinates after pressing a button that chances are you don't already have an existing GPS location stored. You should be using requestLocationUpdates and not getLastKnownLocation.

locationManager.requestLocationUpdate(LocationManager.NETWORK_PROVIDER, 5000, 0, locationListener)

When you use requestLocationUpdate it will automatically call onLocationChanged for you so you don't need to call it in your code.

You can substitute LocationManager.NETWORK_PROVIDERfor LocationManager.GPS_PROVIDER but as long as you have a WiFi connection you should be able to get coordinates.

Salah Assana
  • 186
  • 4
1

If you are trying to use this inside try switching GPS_PROVIDER to NETWORK_PROVIDER; this will work anywhere that the phone has service.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

Because Android doesn't track location when no app is requesting it, in order to save battery. GetLastKnownLocation will almost always return null. If you want an assured non-null response, use requestLocationUpdates or requestSingleLocation. Both of those are asynchronous though, so they will call a callback when a location is found (actually figuring out a location can take from a few seconds to a minute or two, depending on the type of location provider, atmospheric conditions, line of sight issues, etc. If using GPS and inside it could actually never occur.)

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I've never seen that before. I tried to change it to `Location location = locationManager.requestLocationUpdates("gps", 5000, 0, locationListener)` but it Android Studios drew a run line under it and says `Incompatible types` – Natasha Alexandrov Mar 26 '17 at 23:08
  • THe immediately returned value won't help. ITs the one you get when the location listener callback is called that's assure non-null, because the requestXXX functions actually turn on tracking. getLastKnownLocation can be checked and used as an optimization, but shouldn't be relied upon, and defintiely shouldn't be used to check if their location is changing. – Gabe Sechan Mar 26 '17 at 23:10
  • Sorry I don't understand, what's wrong with my implementation? What should I change? – Natasha Alexandrov Mar 26 '17 at 23:14
  • First parameter should be LocationManager.GPS_PROVIDER. It does not return a value- the locationListener will later be called with a value when one is ready. – Gabe Sechan Mar 26 '17 at 23:16
  • So I shouldn't be setting it equal to anything. However now that I've done that my app just crashes and nothing is being printed to the log. The last message before the app terminated was `Connected to process 5726 on device asus-nexus_7-06df827f` – Natasha Alexandrov Mar 26 '17 at 23:25