0

I am trying to display the speed of the user on google glass live card. I am able to get latitude and longitude,But getSpeed() always returns 0.0. I have checked similar questions on SO ,but of no help.

Here is my Code

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        String provider = mLocationManager.getBestProvider(criteria, true);
        boolean isEnabled = mLocationManager.isProviderEnabled(provider);
        if (isEnabled) {
        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        if (location != null) {
        Geocoder geocoder = new Geocoder(Start_service.this.getBaseContext(), Locale.getDefault());
        // lat,lng, your current location
        List<Address> addresses = null;
        try {
            lati= location.getLatitude();
            longi=location.getLongitude();
            speed=location.getSpeed();
        addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        }
        catch (IOException e) {
        System.out.println(e);
        e.printStackTrace();
        }
Prasanna Aarthi
  • 3,439
  • 4
  • 26
  • 48

1 Answers1

1

Location providers don't guarantee to provide the speed value. You can only getSpeed from the provider that calls setSpeed. You can set the Criteria variable to indicate that you need the speed value.

Criteria criteria = new Criteria();
criteria.setSpeedRequired(true);

Or you might consider to calculate it yourself. See why getSpeed() always return 0 on android.

Also, use hasSpeed() to check if a speed is available.

Community
  • 1
  • 1
pt2121
  • 11,720
  • 8
  • 52
  • 69