Search code examples
geolocationcodenameone

How can I interrupt getCurrentLocation method (Codename One Location Manager)?


My app features a map and when the user long presses on it, I need the current phone location to appear on the map. As @Chen advised it looks like getCurrentLocation() is the proper method to use in my case. Please note that I don't want to use a LocationListener because I want the user to have control on when they want to update their location.

Moreover I chose this manual location update because @Shai-Almog discouraged location polling. So after reading this post I first set the timeout to 20 s as written below to get the current phone location :

                Dialog ip = new InfiniteProgress().showInifiniteBlocking();
                Location currentLocation = locationManager.getCurrentLocationSync(
                        ParametresGeneraux.LOCATION_TIMEOUT_MILLI);
                // We try to get something
                if (currentLocation == null) {
                    currentLocation = locationManager.getLastKnownLocation();
                }
                ip.dispose();

But it was not sufficient and the last known location was "always" provided (I did not count maybe 5 times in a row) although the little location icon appeared in the toolbar. So I set it to 40 s but the actual current location did not come either after this twice as big amount of time (only the last known location which is > 2km away was provided).

However if I start Google Maps on Android (KitKat), the location is instantaneously accurate. And then if I go back to my app and press the map the current location is found. Actually I found a post dealing with a difference in accuracy between Google Maps and Android API but it was pertaining to Shangai / China, not the EU (France).

Consequently my question is twofold :

  1. first how can there be such a huge difference on the same device between Google Maps and Codename One ?

  2. then if I don't set a timeout to getCurrentLocation() how can I interrupt the location searching when the user is fed up waiting ?

Any help appreciated,

BR


Solution

  • Instant location will work if the Google Maps location services is turned on. If you enabled or disabled one of the android.playService build hints you must also enable android.playService.location=true to get the fast location service used by maps.