0

I have a program that gets latitude and longitude of the user.

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

        // Create a criteria object to retrieve provider
        Criteria criteria = new Criteria();
        // Get the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);
        // Get Current Location
        if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
        }
        Location myLocation = locationManager.getLastKnownLocation(provider);
    Location myLocation = locationManager.getLastKnownLocation(provider);
                boolean netEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                if (netEnabled) {
                    if (isGPSEnabled) {
                        latitude = myLocation.getLatitude();

                        longitude = myLocation.getLongitude()
                    } else if (!isGPSEnabled) {
                        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
                        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();

                        } else {


                            // displayPromptForEnablingGPS(MapsActivity.this);
                        }
                    }
                } else if (!netEnabled) {


                    if (isGPSEnabled) {
                        latitude = myLocation.getLatitude();

                        // Get longitude of the current location
                        longitude = myLocation.getLongitude();


                    } else {
                        // displayPromptForEnablingGPS(MapsActivity.this);
                    }

                }

(displayPromptForEnablingGPS is for ACTION_LOCATION_SOURCE_SETTINGS)

In the program, if GPS and network providers are off, a window is opened for switching on them. When i switch GPS on in the first time, it gives error in first if(GPSEnabled) method(error on latitude=myLocation.getLatitude()). I can't get latitude and longitude. It is also same when i switch on only network provider. It gives "location" variable null. But if i open Google Maps, it starts to get latitude and longitude.

What can the problem be? It is problem of phone or i must make GPS run by writing any code?

Thanks for your help.

Ugur
  • 3
  • 4

1 Answers1

0

The method will return null value in case there’s no location information previously. It’s better to get last known location iterate over all of the enabled providers, because this is the case that there’s no GPS and Network provider. >> link on SO It’s just better to add handling that case if(!netEnabled && isGPSEnabled ){ //no network provider is enabled

Above sample code : here

Community
  • 1
  • 1
adjuremods
  • 2,938
  • 2
  • 12
  • 17