简体   繁体   中英

android- How to update the location without opening the maps/navigation app

I want to get the exact & accurate current Location. For this, I used the location manager requestLocationUpdates() method but for invoking the onLocationChanged() method will take so much time. So I set the timer as 20sec for that. I mean onLocationChanged() method is not invoked even after 20sec then I decided to took the last known location. Here I am facing the problem. getLastKnownLocation() returns null.

But I want the location. Here I found the one solution for this. It is because of testing device don't have recent location update with the gps provider. So we need to manually open the Maps/Navigation app then it will return the location. what should we need to do for getting the location instead of opening the Maps app. I think we should do same as Maps app do for location update. How do we implement like that without opening it.

// Get the Current Location using requestLocationUpdates

public void getLocation() {

    locationManager.requestLocationUpdates(strProvider, 0, 0,
            CurrentlocationListener);

}

LocationListener CurrentlocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if (location != null) {

            locationManager.removeUpdates(this);
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            String strCurrentLatitude = String.valueOf(lat);
            String strCurrentLongitude = String.valueOf(lng);

            System.out
                        .println("Current Latitude and Longitude(onLocation Changed): "
                                + strCurrentLatitude
                                + ","
                                + strCurrentLongitude);
        }
    }

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

//Get the current location using the lastknownlocation.

   String location_context = Context.LOCATION_SERVICE;

    locationManager = (LocationManager) getSystemService(location_context);
    Criteria crit = new Criteria();
    crit.setAccuracy(Criteria.ACCURACY_FINE);
    strProvider = locationManager.getBestProvider(crit, true);
    Location location = locationManager
                        .getLastKnownLocation(strProvider);
                System.out.println("location(geoPoint1): " + location);

                String strCurrentLatitude = "0", strCurrentLongitude = "0";

                if (location != null) {
                    strCurrentLatitude = String.valueOf(location
                            .getLatitude());
                    strCurrentLongitude = String.valueOf(location
                            .getLongitude());
                }

Whatever you are doing in OnLocationChanged() is correct. Now replace code of LocationManager with this. It'll work.

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();

            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);

            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);

            if (location != null) {
                onLocationChanged(location);



            }

            locationManager.requestLocationUpdates(provider, 1000, 0, this);

        }

Thanks. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM