Smart Blood Donor
Smart Blood Donor

Reputation: 1

Android - Is there a way to get latitude and longitude using network provider if location settings is switched off

My problem is that my code is not taking location from GPS but always from Network provider

I have added these two lines

Log.i("GPSTracker GPS","lat : " + latitude + ", long : " + longitude);
Log.i("GPSTracker Net","lat : " + latitude + ", long : " + longitude);

in my following code to check what my app is using to get location

while

Log.i("GPSTracker GPS","lat : " + latitude + ", long : " + longitude);

is never invoked

My entire code is following

    public Location getLocation() {
    try {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (isGPSEnabled || isNetworkEnabled) {
            this.canGetLocation = true;
            if (isGPSEnabled) {
                if (location == null) {
                    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_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_tab grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                                Manifest.permission.ACCESS_FINE_LOCATION)) {

                            ActivityCompat.requestPermissions((Activity) context,
                                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                    PackageManager.PERMISSION_GRANTED);
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                                ActivityCompat.requestPermissions((Activity) context,
                                        new String[]{Manifest.permission.LOCATION_HARDWARE},
                                        PackageManager.PERMISSION_GRANTED);
                            }
                        }
                    }
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES,
                            this);
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            Log.i("GPSTracker GPS","lat : " + latitude + ", long : " + longitude);
                        }
                    }
                }
            }
            if (isNetworkEnabled) {

                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.INTERNET)
                        != 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_tab grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                            Manifest.permission.INTERNET)) {

                        ActivityCompat.requestPermissions((Activity) context,
                                new String[]{Manifest.permission.INTERNET},
                                PackageManager.PERMISSION_GRANTED);
                    }
                    if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                            Manifest.permission.ACCESS_COARSE_LOCATION)) {

                        ActivityCompat.requestPermissions((Activity) context,
                                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                PackageManager.PERMISSION_GRANTED);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                            ActivityCompat.requestPermissions((Activity) context,
                                    new String[]{Manifest.permission.LOCATION_HARDWARE},
                                    PackageManager.PERMISSION_GRANTED);
                        }
                    }
                }
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES,
                        this);
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        Log.i("GPSTracker Net","lat : " + latitude + ", long : " + longitude);
                    }
                }
            }

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

My manifest file is

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

I want my app to take location from GPS when location is switched ON and get latitude and longitude from network provider if location from settings is switched off - if there is a way

Upvotes: 0

Views: 976

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93542

There's 2 reasons.

1)YOur code does things in the wrong order. It checks gps, then network. So if both are enabled (which is 99% of the time) the gps will be overwritten.

2)GPS takes time. YOu're trying to call getLastKnownLocation, which almost never works. If you want a GPS location, wait for the callback.

Side note: this code is horribly flawed in muliple ways. GPSTracker is badly broken. Don't use it. See why at http://gabesechansoftware.com/location-tracking/

Upvotes: 1

Related Questions