loreeemartiii
loreeemartiii

Reputation: 385

User location in Codename One

Our goal is to obtain maximum accuracy (gps) if possible with a 30 seconds timeout and then if this operation fails, we want to get the location with less accuracy (network) with a 10 seconds timeout as a last resort (if even the less accurated location is not provided) we want to get last know position

We know that in codename one exists two way of querying location manager

1) by using getCurrentLocationSync:

Location position = LocationManager.getLocationManager().getCurrentLocationSync();

but in this way we don't know how to set accuracy using that API (you confirm that there isn't a way of setting accuracy in this way?)

2) by using setLocationListener:

public MyListener implements LocationListener {
    public void locationUpdated(Location location) {
    // update UI etc.
    }



    public void providerStateChanged(int newState) {
    // handle status changes/errors appropriately
    }
}
LocationManager.getLocationManager().setLocationListener(new MyListener());

but even using this we have only gps position

As an example: the previous version of our application ( using Apache Cordova ) gets the position using this method:

this._getPosition = function(cb,err_cb){
        this._open_loading("Ricerca posizione...");
        err_cb = err_cb || function(){};
        first_err_cb = function(d){
                self.options.locationService.getCurrentPosition(
                    function(pos){
                        self._close_loading();
                        cb(pos); 
                    },
                    function(e){ 
                        self._close_loading();
                        err_cb(e); 
                    },
                    { maximumAge: 30000, timeout: 10000, enableHighAccuracy: false }
                );
            };




            self.options.locationService.getCurrentPosition(
            function(pos){
                self._close_loading();
                cb(pos);
            },
            first_err_cb,
            { maximumAge: 30000, timeout: 20000, enableHighAccuracy: true }
        );
    };

Upvotes: 0

Views: 49

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

setLocationListener uses hybrid location and will work with the native device location API which means it will use whatever the device uses including GPS and other sensors. In iOS this is seamless. In Android if you manipulated build hints in some way this can be inadvertently disabled in which case you will fallback to legacy GPS only code.

Notice you can use the setLocationListener(LocationListener, LocationRequest) variant of the method to configure accuracy etc. We don't recommend using the simpler synchronous location API's as their implementation is naive.

To make sure the play services location API will be used in Android you can use the build hint: android.playService.location=true.

Upvotes: 1

Related Questions