0

I have done this code where I want to get longitude and latitude , but using both network provider and gps provider i get my location null . even if gps is enabled ... why so?

      boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
      boolean  isNetworkEnabled = lm
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        Location location = null;
        if (!isGPSEnabled && !isNetworkEnabled)
        {  
        }
        else if(isGPSEnabled ) {
            location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(location == null)
            {                   
                if(isNetworkEnabled)
                    location =                                   lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);                 
                if(location == null)
                {
                }
            }
        } else if(isNetworkEnabled){
            location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);   
            if(location == null)
            {
            }
        }
user3278732
  • 1,694
  • 10
  • 31
  • 67
  • Duplicate question http://stackoverflow.com/questions/12597076/network-provider-and-gps-provider-returning-null-values?rq=1 – Tugrul Mar 26 '14 at 11:38
  • There is something in new version of Android... are you using device or emulator. if device what is its version. – Attiq ur Rehman Mar 26 '14 at 11:39

1 Answers1

0

there are several reason of not getting co ordinates but incase of yours, since your gps is enabled (remember gps takes alot of time to get coordinates) and you are checking for gps first in your code, it is not go to the network part because you are using if else.so change your code to like this and check for network first.

if (!isGPSEnabled && !isNetworkEnabled)
        {  
        }
      else if(isNetworkEnabled){
            location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);   
            if(location == null)
            {
            }
        }
        else if(isGPSEnabled ) {
            location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(location == null)
            {                   
                if(isNetworkEnabled)
                    location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);                 
                if(location == null)
                {
                }
            }
        } 
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45