Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the speed of a vehicle using GPS in android

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) {}      } } 
like image 527
John Kulova Avatar asked Mar 22 '13 12:03

John Kulova


People also ask

What app calculates speed?

Ulysse Speedometer is one of the best speedometer apps for android 2020. This feature-rich app is a GPS speedometer that shows your speed in both digital and analog forms. It has other great features, which include displaying direction, speed, battery charge, and time.


2 Answers

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.

like image 163
Samuel Avatar answered Sep 30 '22 16:09

Samuel


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)

like image 36
ISURU Avatar answered Sep 30 '22 16:09

ISURU