简体   繁体   中英

How to get user location, using GPS

I need to get user location, when application launched for set marker on Google Map. I found this variant for find user location:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            //Get location and unsubscribe
            locationManager.removeUpdates(this);
            ...
        }
   ...
}

Such method doesn't always work. Usually, application wait for 30 sec - 2 min. But sometimes this doesn't work and I can't take listener response.


How to get user location and take 100% response (if this possibly, when device normal work)?

You can use LocationManager.NETWORK_PROVIDER. It is less accurate comparing to GPS but it is more available.

While you wait for the initial "real" location update to complete, you can get the last location known to the device using

LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

This doesn't guarantee that you will have the most up-to-date location, but it can hopefully give you something relevant to display in the meantime.

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