Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Track Users Speed Using location.getSpeed() GPS Method in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 10.4k
    Comment on it

    In this tutorial, we will get to know that how we can get our speed using GPS in android, we get the speed in onLocationChanged(Location location) method that is our @overrided method whose parameter that is Location here helps us to give speed in meter/second. So here is the line that gives speed location.getSpeed(), the value is long type that we get from this code and the unit is meter/second.

     

     

    For enabling GPS in your app we need to give run-time permission for ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION.

    Copy the code given below in your activity or if you are wishing to get speed even when your app is in the background the add the code in your service class.

    protected LocationManager locationManager;
    
        // The minimum distance to change Updates in meters
        private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1   ; //10*1 10 meters
    
        // The minimum time between updates in milliseconds
        private static final long MIN_TIME_BW_UPDATES = 1000 * 1; // 1 second
    public Location getLocation() 
    {
       try {
                locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
     if (isGPSEnabled) {
                        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
    
                        }
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    } catch (Exception e) {
                e.printStackTrace();
            }
    
            return location;
        }
    
    
     @Override
        public void onLocationChanged(Location location) {
    
            if (location != null) {
    
    
                this.location = location;
                double dSpeed = location.getSpeed();
               
            }
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
    
        }
    
        @Override
        public void onProviderEnabled(String provider) {
    
        }
    
        @Override
        public void onProviderDisabled(String provider) {
    
        }
    
    

     

    Now if you want to change the speed from meter/second to miles/hour or km/hour then copy the code given below and use it wherever you wish to.

    private void speedUnit(long speed)
    { 
    
    //here speed is the value extracted from speed=location.getSpeed();
         //for km/hour
                double a = 3.6 * (event);
                int kmhSpeed = (int) (Math.round(speed));
                
          
        //for mile/hour
                double a = 2.23694 * (event);
                int mileSpeed = (int) (Math.round(speed));
                
            
    }

     

    How to Track Users Speed Using location.getSpeed() GPS Method in Android

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: