Taimoor
Taimoor

Reputation: 27

LocationManager.NETWORK_PROVIDER is not working

I am trying to get latitude and longitude without gps but it gives me null. But when I on my gps, its giving correct values of lat long. I want to get latitude and longitude by wifi. How can I solve this? Here is my code for network provider:

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);
        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
            return null;
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
                     return null;
                }
                //updates will be send according to these arguments
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
                        return null;
                    }
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

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

Upvotes: 0

Views: 2281

Answers (3)

Saurav
Saurav

Reputation: 560

The link provided has all the information. However to fetch location from Lat and long we need to use GeoCoder.

Geocoder g1= new Geocoder(getApplicationContext());
        List<Address> locale= null;
         String add = null;
        try
        {
            locale = g1.getFromLocation(location.getLatitude(), location.getLongitude(), 3);
        }
        catch(Exception e)
        {
        Log.d("Saurav_log", "Cant fetch address");
        }
    /*  finally
        {
         Log.d("Saurav_log", "Finally Exiting");
        }*/
           TextView textView2 = (TextView)findViewById(R.id.textView2);
           if(locale != null)
           {
              add =locale.get(0).getAddressLine(0);
           }
textView2.setText("From GPS:Actual Address \n"+add+locale.get(0).getLocality()+" \n"+locale.get(0).getCountryName()+" \n"+locale.get(0).getSubLocality());

Upvotes: 0

Saurav
Saurav

Reputation: 560

Pls query if network provider is enabled. You can check if network_provider is enabled using: locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

You can request for location updates using : locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);

You can get last known location using: Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

I have a sample application in my github repo which used to work. No recent updates though. Here's is the link: https://github.com/sauravpradhan/TrackMe-Ver-2.0

Please get back if this helps.

Upvotes: 0

Teyam
Teyam

Reputation: 8082

Try using WifiManager class.

As mentioned in the documentation,

This class provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling Context.getSystemService(Context.WIFI_SERVICE).

This is the API to use when performing Wi-Fi specific operations.

Also, check solution given in this SO post - Android: How to Enable/Disable Wifi or Internet Connection Programmatically. Hope it helps!

Upvotes: 1

Related Questions