Determining the speed of a vehicle using GPS in android

84,161

Solution 1

GPS works fine in a vehicle. The NETWORK_PROVIDER setting might not be accurate enough to get a reliable speed, and the locations from the NETWORK_PROVIDER may not even contain a speed. You can check that with location.hasSpeed() (location.getSpeed() will always return 0).

If you find that location.getSpeed() isn't accurate enough, or it is unstable (i.e. fluctuates drastically) then you can calculate speed yourself by taking the average distance between a few GPS locations and divide by the time elapsed.

Solution 2

for more information onCalculate Speed from GPS Location Change in Android Mobile Device view this link

Mainly there are two ways to calculate the speed from mobile phone.

  1. Calculate speed from Accelerometer
  2. Calculate speed from GPS Technology

Unlike Accelerometer from GPS Technology if you're going to calculate speed you must enable data connection and GPS connection.

In here we are going to calculate speed using GPS connection. In this method we using how frequency the GPS Location points are changing during single time period. Then if we have the real distance between the geo locations points we can get the speed. Because we have the distance and the time. Speed = distance/time But getting the distance between two location points is not very easy. Because the world is a goal in shape the distance between two geo points is different from place to place and angle to angle. So we have to use “Haversine Algorithm”

enter image description here

First we have to give permission for Get Location data in Manifest file

Make the GUI enter image description here

enter image description here

enter image description here

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtCurrentSpeed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="000.0 miles/hour"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <CheckBox android:id="@+id/chkMetricUnits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Use metric units?"/>

Then make an interface to get the speed

package com.isuru.speedometer;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

public interface IBaseGpsListener extends LocationListener, GpsStatus.Listener {

      public void onLocationChanged(Location location);

      public void onProviderDisabled(String provider);

      public void onProviderEnabled(String provider);

      public void onStatusChanged(String provider, int status, Bundle extras);

      public void onGpsStatusChanged(int event);

}

Implement the logic to get the speed using the GPS Location

import android.location.Location;

public class CLocation extends Location {

      private boolean bUseMetricUnits = false;

      public CLocation(Location location)
      {
            this(location, true);
      }

      public CLocation(Location location, boolean bUseMetricUnits) {
            // TODO Auto-generated constructor stub
            super(location);
            this.bUseMetricUnits = bUseMetricUnits;
      }


      public boolean getUseMetricUnits()
      {
            return this.bUseMetricUnits;
      }

      public void setUseMetricunits(boolean bUseMetricUntis)
      {
            this.bUseMetricUnits = bUseMetricUntis;
      }

      @Override
      public float distanceTo(Location dest) {
            // TODO Auto-generated method stub
            float nDistance = super.distanceTo(dest);
            if(!this.getUseMetricUnits())
            {
                  //Convert meters to feet
                  nDistance = nDistance * 3.28083989501312f;
            }
            return nDistance;
      }

      @Override
      public float getAccuracy() {
            // TODO Auto-generated method stub
            float nAccuracy = super.getAccuracy();
            if(!this.getUseMetricUnits())
            {
                  //Convert meters to feet
                  nAccuracy = nAccuracy * 3.28083989501312f;
            }
            return nAccuracy;
      }

      @Override
      public double getAltitude() {
            // TODO Auto-generated method stub
            double nAltitude = super.getAltitude();
            if(!this.getUseMetricUnits())
            {
                  //Convert meters to feet
                  nAltitude = nAltitude * 3.28083989501312d;
            }
            return nAltitude;
      }

      @Override
      public float getSpeed() {
            // TODO Auto-generated method stub
            float nSpeed = super.getSpeed() * 3.6f;
            if(!this.getUseMetricUnits())
            {
                  //Convert meters/second to miles/hour
                  nSpeed = nSpeed * 2.2369362920544f/3.6f;
            }
            return nSpeed;
      }



}

Combine logic to GUI

import java.util.Formatter;
import java.util.Locale;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements IBaseGpsListener {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            this.updateSpeed(null);

            CheckBox chkUseMetricUntis = (CheckBox) this.findViewById(R.id.chkMetricUnits);
            chkUseMetricUntis.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                  @Override
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub
                        MainActivity.this.updateSpeed(null);
                  }
            });
      }

      public void finish()
      {
            super.finish();
            System.exit(0);
      }

      private void updateSpeed(CLocation location) {
            // TODO Auto-generated method stub
            float nCurrentSpeed = 0;

            if(location != null)
            {
                  location.setUseMetricunits(this.useMetricUnits());
                  nCurrentSpeed = location.getSpeed();
            }

            Formatter fmt = new Formatter(new StringBuilder());
            fmt.format(Locale.US, "%5.1f", nCurrentSpeed);
            String strCurrentSpeed = fmt.toString();
            strCurrentSpeed = strCurrentSpeed.replace(' ', '0');

            String strUnits = "miles/hour";
            if(this.useMetricUnits())
            {
                  strUnits = "meters/second";
            }

            TextView txtCurrentSpeed = (TextView) this.findViewById(R.id.txtCurrentSpeed);
            txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits);
      }

      private boolean useMetricUnits() {
            // TODO Auto-generated method stub
            CheckBox chkUseMetricUnits = (CheckBox) this.findViewById(R.id.chkMetricUnits);
            return chkUseMetricUnits.isChecked();
      }

      @Override
      public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if(location != null)
            {
                  CLocation myLocation = new CLocation(location, this.useMetricUnits());
                  this.updateSpeed(myLocation);
            }
      }

      @Override
      public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

      }

      @Override
      public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

      }

      @Override
      public void onGpsStatusChanged(int event) {
            // TODO Auto-generated method stub

      }



}

If you want to convert Meters/Second to kmph-1 then you need to multipl the Meters/Second answer from 3.6

Speed from kmph-1 = 3.6 * (Speed from ms-1)

Solution 3

public class MainActivity extends Activity implements LocationListener {

add implements LocationListener next to Activity

LocationManager lm =(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        this.onLocationChanged(null);

LocationManager.GPS_PROVIDER, 0, 0, The first zero stands for minTime and the second one for minDistance in which you update your values. Zero means basically instant updates which can be bad for battery life, so you may want to adjust it.

     @Override
    public void onLocationChanged(Location location) {

    if (location==null){
         // if you can't get speed because reasons :)
        yourTextView.setText("00 km/h");
    }
    else{
        //int speed=(int) ((location.getSpeed()) is the standard which returns meters per second. In this example i converted it to kilometers per hour

        int speed=(int) ((location.getSpeed()*3600)/1000);

        yourTextView.setText(speed+" km/h");
    }
}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}


@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}


@Override
public void onProviderDisabled(String provider) {


}

Don't forget the Permissions

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Solution 4

We can use location.getSpeed();

  try {
                // Get the location manager
                double lat;
                double lon;
                double speed = 0;
                LocationManager locationManager = (LocationManager)
                        getActivity().getSystemService(LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                String bestProvider = locationManager.getBestProvider(criteria, false);
                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                Location location = locationManager.getLastKnownLocation(bestProvider);
                try {
                    lat = location.getLatitude();
                    lon = location.getLongitude();
                    speed =location.getSpeed();
                } catch (NullPointerException e) {
                    lat = -1.0;
                    lon = -1.0;
                }

                mTxt_lat.setText("" + lat);
                mTxt_speed.setText("" + speed);

            }catch (Exception ex){
                ex.printStackTrace();
            }
Share:
84,161
John Kulova
Author by

John Kulova

Developer

Updated on February 20, 2020

Comments

  • John Kulova
    John Kulova about 4 years

    I would like to know how to get the speed of a vehicle using your phone while seated in the vehicle using gps. I have read that the accelerometer is not very accurate. Another thing is; will GPS be accessible while seated in a vehicle. Won't it have the same effect as while you are in a building?

    Here is some code I have tried but I have used the NETWORK PROVIDER instead.I will appreciate the help. Thanks...

    package com.example.speedtest;
    
    import android.app.Activity;
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        LocationManager locManager;
        LocationListener li;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            locManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
            li=new speed();
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, li);
        }
        class speed implements LocationListener{
            @Override
            public void onLocationChanged(Location loc) {
                Float thespeed=loc.getSpeed();
                Toast.makeText(MainActivity.this,String.valueOf(thespeed), Toast.LENGTH_LONG).show();
            }
            @Override
            public void onProviderDisabled(String arg0) {}
            @Override
            public void onProviderEnabled(String arg0) {}
            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
    
        }
    }
    
  • John Kulova
    John Kulova about 11 years
    Thanx, I had thought of calculating the speed manually until I found the convinience function. Well NETWORK provider was giving me a 0.0 value. I guess this means it has no speed. Thanks again.
  • Prasad
    Prasad about 7 years
    what will be units for location.getSpeed() ? km/h, M/h, ft/sec, mtrs/sec
  • Samuel
    Samuel about 7 years
    The documentation says meters per second
  • Tushar Gogna
    Tushar Gogna over 5 years
    Using this, there is a delay in getting speed as onLocationChanged is called after sometime each time. What can we do to make it flawless?
  • Kathir
    Kathir over 3 years
    I extended the android.location.LocationListener ( com.google.android.gms.location.LocationListener is also available)