When you start the application, you need to get the user's location on the map. Googling and looking in the book, the only thing I found was to use the distance change listener:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() { @Override public void onLocationChanged(Location location) { //Получаю местоположение и отписываюсь locationManager.removeUpdates(this); ... } ... } 

This method does not always work. Sometimes the application waits 10 seconds or 2 minutes before the listener works. Sometimes it just does not work (if the phone does not move).


Is there any 100% working alternative to request the current distance (under conditions of normal GPS operation), and not wait for a response to the listener changes?

  • I doubt that such a solution really exists. There will always be some but . Perhaps, if you only need a location once, then you need not a listener to change it, but FusedLocationProviderApi from whom to request the last known location - YuriiSPb
  • @Yuriy SPb, this may not work. If I get the wrong point, the car will go wrong. In case of not finding a place, I will ask you to enter it manually. - Rostislav Dugin

3 answers 3

Why do you request GPS_PROVIDER from the provider? GPS warms up for a long time with a cold start and because of this there are delays in 20-120 seconds. If you use the coordinates of the network / Wi-Fi, then the location can be obtained almost instantly.

Try reactive-location , there is an example of getting a location with a timeout, you get a good flow for your case:

 LocationRequest req = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setExpirationDuration(5000) .setInterval(1000); Observable<Location> goodEnoughQuicklyOrNothingObservable = new ReactiveLocationProvider(getContext()) .getUpdatedLocation(req) .filter(location -> location.getAccuracy() < 50) // с точностью до 50 метров .timeout(5, TimeUnit.SECONDS, Observable.just(null), AndroidSchedulers.mainThread()) .first() .observeOn(AndroidSchedulers.mainThread()); Subscription subscription = goodEnoughQuicklyOrNothingObservable .subscribe(location -> { if (location == null) { // не удалось получить координаты, // требуется ввод вручную } else { // получили координаты } }, Timber::e); 
  • I will try tomorrow, today I have already folded my laptop. But immediately the question: what if you increase the accuracy to 5-10 meters, it will affect the speed? - Rostislav Dugin
  • @RostislavDugin, checked in my GPS mode "on all GPS / Wi-Fi / LTE coordinates", the accuracy was first ~ 30 meters, then within 30 seconds it fell to ~ 10-15 and remained at this level - EgorD
  • In the near future I may not have time to try your option. If I do not have time, I will accept your answer and give the reward :). - Rostislav Dugin
  • With accuracy (filter), I cannot find less than 50 meters at all, but with accuracy <= 50 , almost immediately. - Rostislav Dugin

After a week of searching and the question on enSO and ruSO, I came to the warrant that there are no alternatives.

The location can only be determined using LocationManager.requestLocationUpdates and get the latest location that the phone remembered using the FocusedLocationManagerApi method .


Alternatively, you can prompt the user to select a location through the marker or enter it manually.

This will help auto-enter the location and location via coordinates.

    Alternatively, you can suggest that you manually enter the location. It would also be nice to require the user to turn on Wi-Fi , for more accurate positioning. And getting coordinates via Wi-Fi / A-GPS will work much faster - even instantly. Indeed, unlike GPS, time is not wasted searching for satellites.

    • one
      I wrote this in response. Why repeat? - Rostislav Dugin
    • @RostislavDugin I actually have another answer. I do not understand why you are minus. Can I and confirmed your views in the first sentence, but for what are you minus? - user232857
    • For the fact that you simply copied my answer by changing three sentences, relying on some kind of plus in reputation. - Rostislav Dugin
    • @AndreyYanov The first version of your answer contained a fragment identical to the fragment of the answer Rostislav. More such text is nowhere to google, so it is unlikely that you both copied from one place. Therefore, it looks like you just copied a part of the answer without specifying the source. No need to do so, no good. - Nick Volynkin
    • @NickVolynkin answer corrected, it is not necessary to dizlaykat - user232857