Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get the current latitud and longitud without use getLastKnownLocation method in Android?

I am trying to get the current position of the phone, for this I use the GPSTracker tutorial, the problem is always use the method getLastKnownLocation and return older position.

                if (isNetworkEnabled) {
                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) {
                    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();
                        }
                    }
                }
            }

You can see the tutorial use getLastKnownLocation when use Network or GPS provider, but I realy realy need the current position.

I dont know how get it!

like image 259
TuGordoBello Avatar asked Dec 25 '22 22:12

TuGordoBello


2 Answers

Use Fused Location Provider APIs in conjunction with GoogleApiClient. You can find the apis here. They are designed to give locations with high accuracy. Use it using GoogleApiClient.

new GoogleApiClient.Builder(context)
         .addApi(LocationServices.API)
         .addConnectionCallbacks(this)
         .addOnConnectionFailedListener(this)
         .build()

There are other approaches to using location providers. You should not restrict yourself to locations obtained using GPS and Mobile Network. Rather iterate over all the location providers and depending on your accuracy requirement choose those which give you high accuracy. The following code does this:

    Location bestResult = null;
    float bestAccuracy = Float.MAX_VALUE;
    long bestAge = Long.MIN_VALUE;
    List<String> matchingProviders = mLocationManager.getAllProviders();

    for (String provider : matchingProviders) {

        Location location = mLocationManager.getLastKnownLocation(provider);

        if (location != null) {

            float accuracy = location.getAccuracy();
            long time = location.getTime();

            if (accuracy < bestAccuracy) {

                bestResult = location;
                bestAccuracy = accuracy;
                bestAge = time;

            }
        }
    }

    // Return best reading or null
    if (bestAccuracy > minAccuracy
            || (System.currentTimeMillis() - bestAge) > maxAge) {
        return null;
    } else {
        return bestResult;
    }

You can also use the location object directly after checking them for accuracy. You should take advantage of the locations accessed by other apps and not just rely on the locations obtained by GPS/Mobile Networks.

If you want to continuously listen to location updates implement the Locationlistener callbacks to continuously get location updates.

like image 127
Roadblock Avatar answered Dec 28 '22 08:12

Roadblock


If I recall correctly, that tutorial is rather old and do not use fused locations. In case you develop in Android Studio, consider trying this library: https://github.com/mcharmas/Android-ReactiveLocation

Way less code and fused location using the latest API.

To get the last known location simply:

ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);
locationProvider.getLastKnownLocation()
    .subscribe(new Action1<Location>() {
        @Override
        public void call(Location location) {
            // doSthImportantWithObtainedLocation(location);
        }
    });

Thats it!

like image 44
cYrixmorten Avatar answered Dec 28 '22 07:12

cYrixmorten