Geolocation - Location in android

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have used GPS in measuring distance traveled.The problem is GPS sometimes do not update location change for a long time.

Is there any solution for this?

    locationListen = new LocationListener() {

        public void onLocationChanged(Location currentLocation) {
            if (prevLocation != null){
                test = currentLocation.distanceTo(prevLocation);
                distance+=test;
            }
            prevLocation = currentLocation;

        }

        public void onStatusChanged(String provider, int status, Bundle b) {
            switch (status) {
            case LocationProvider.OUT_OF_SERVICE:
                Toast.makeText(MainActivity.this,
                        "Status Changed: Out of Service " + provider,
                        Toast.LENGTH_SHORT).show();
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                Toast.makeText(
                        MainActivity.this,
                        "Status Changed: Temporarily Unavailable "
                                + provider, Toast.LENGTH_SHORT).show();
                break;
            case LocationProvider.AVAILABLE:
                Toast.makeText(MainActivity.this,
                        "Status Changed: Available " + provider,
                        Toast.LENGTH_SHORT).show();
                break;
            }
        }

        public void onProviderDisabled(String s) {
        }

        public void onProviderEnabled(String s) {
        }
    };

    startOperation.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View but) {
            if (startOperation.getText().equals("Start Calculating Fare")) {
                mHandler.post(run);
                myManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER, 1000, 10,
                                locationListen);
                startOperation.setText("Stop the calculations");
            } else {
                mHandler.removeCallbacks(run);
                myManager.removeUpdates(locationListen);
                finalInfos();
            }
        }
    });

My code for the location listener. It just update using GPS. As what I am saying the GPS is sometimes too long to detect a location

Answers

You are explicitly requesting a GPS-only location provider in your code. This forces Android to rely on GPS alone, with all of its drawbacks (GPS requires a clean view of the sky to work, for example).

To get a more reliable device tracking, you have to use a more reliable and flexible location strategy, changing the current location provider accordingly to the changed external scenario (indoor/outdoor, etc.). Even better, you could use a whole set of different location providers at the same time (that is: a "hybrid" positioning system).

Have a look at these previously answered SO questions:

http://stackoverflow.com/questions/9785768/more-than-one-location-provider-at-same-time http://stackoverflow.com/questions/9785768/more-than-one-location-provider-at-same-time

http://stackoverflow.com/questions/4150462/using-requestlocationupdates-for-more-than-one-provider http://stackoverflow.com/questions/4150462/using-requestlocationupdates-for-more-than-one-provider

And to these pages of the Android documentation:

http://developer.android.com/guide/topics/location/strategies.html http://developer.android.com/guide/topics/location/strategies.html

http://developer.android.com/training/basics/location/locationmanager.html http://developer.android.com/training/basics/location/locationmanager.html

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/12684600/location-in-android

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils