From today’s article we are going to discuss how to
calculate current moving speed of a mobile phone. (Actually we are going to calculate
current moving speed of a mobile phone user through his or her mobile device).
Mainly there are two ways to calculate the speed from mobile
phone .
- ·
Calculate
speed from Accelerometer - ·
Calculate
speed from GPS Technology
Unlike Accelerometer from GPS
Technology if you going to calculate speed you must enable data connection and
GPS connection.
So Today 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 be 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 diastase
between two geo points is different from place to place and angle to angle. So
we have to use “Haversine Algorithm”
<uses -permission
android: name="android. permission. ACCESS_FINE_LOCATION"/>
<uses -permission
android: name="android. permission. ACCESS_COARSE_LOCATION"/>
1.
Make the GUI
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<TextView
<CheckBox android: id="@+id/chkMetricUnits "
1.
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);
}
2.
Implement the logic to get the speed using the GPS Location
package com.isuru.speedometer;
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
}
{
}
{
}
@Override
// TODO
Auto-generated method stub
{
//Convert
meters to feet
}
}
@Override
// TODO
Auto-generated method stub
{
//Convert
meters to feet
}
}
@Override
// TODO
Auto-generated method stub
{
//Convert
meters to feet
}
}
@Override
// TODO
Auto-generated method stub
{
//Convert
meters/second to miles/hour
}
}
}
3.
Combine
logic to GUI
@Override
LocationManager
locationManager = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
@Override
//
TODO Auto-generated method stub
}
});
}
{
System. exit( 0);
}
//
TODO Auto-generated method stub
{
location.setUseMetricunits(this.useMetricUnits( ));
}
Formatter
fmt = new Formatter( new StringBuilder( ));
String
strCurrentSpeed = fmt . toString();
String
strUnits = "miles/hour";
if(this.useMetricUnits( ))
{
}
TextView
txtCurrentSpeed = (TextView) this.findViewById(R.id.txtCurrentSpeed);
}
//
TODO Auto-generated method stub
}
@Override
//
TODO Auto-generated method stub
{
}
}
@Override
//
TODO Auto-generated method stub
}
@Override
//
TODO Auto-generated method stub
}
@Override
//
TODO Auto-generated method stub
}
@Override
//
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)