Android -确定用户速度的可能方法



我想问你有没有可能的方法来计算两个位置之间的速度例如,如果用户在位置A,然后移动到位置B,如何知道用户在这两点之间的速度?此外,我能否确定用户是在走路、坐车还是坐火车……等等?

  1. 读取当前位置每30秒或任何间隔你喜欢。
  2. 获取两个位置之间的差异,并使用以下命令转换为英里:
http://www.movable-type.co.uk/scripts/latlong.html

  1. 通过除以你的时间间隔得到这段时间内的速度

通过速度值,您也许可以确定速度是步行(最慢),汽车还是火车(最快)。

如果你的应用程序正在运行,你可以记录GPS传感器的数据来检查速度。或者只是计算用户从A点到b点所需的时间。

全程平均速度为

average speed = distance between A and B / time needed to move from A to B

http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/

获取用户的速度,使用这行代码:

speed = loc.getSpeed();

我将解释如何通过使用Android位置管理器在Android设备中使用GPS获得当前用户的速度。获取当前速度对用户来说有多种用途。

GPSManager.java

public class GPSManager implements android.location.GpsStatus.Listener{
    private static final int gpsMinTime = 500;
    private static final int gpsMinDistance = 0;
    private static LocationManager locationManager = null;
    private static LocationListener locationListener = null;
    private static GPSCallback gpsCallback = null;
    Context mcontext;
    public GPSManager(Context context) {
       mcontext=context;
            GPSManager.locationListener = new LocationListener() {
                    @Override
                    public void onLocationChanged(final Location location) {
                            if (GPSManager.gpsCallback != null) {
                                    GPSManager.gpsCallback.onGPSUpdate(location);
                            }
                    }
                    @Override
                    public void onProviderDisabled(final String provider) {
                    }
                    @Override
                    public void onProviderEnabled(final String provider) {
                    }
                    @Override
                    public void onStatusChanged(final String provider, final int status, final Bundle extras) {
                    }
            };
    }
    public void showSettingsAlert(){
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(mcontext);
        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");
        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
               Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
               mcontext.startActivity(intent);
            }
        });
        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
        });
        // Showing Alert Message
        alertDialog.show();
   }
    public GPSCallback getGPSCallback()
    {
            return GPSManager.gpsCallback;
    }
    public void setGPSCallback(final GPSCallback gpsCallback) {
            GPSManager.gpsCallback = gpsCallback;
    }
    public void startListening(final Context context) {
            if (GPSManager.locationManager == null) {
                    GPSManager.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            }
            final Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setSpeedRequired(true);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            final String bestProvider = GPSManager.locationManager.getBestProvider(criteria, true);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
        }
            if (bestProvider != null && bestProvider.length() > 0) {
                    GPSManager.locationManager.requestLocationUpdates(bestProvider, GPSManager.gpsMinTime,
                                    GPSManager.gpsMinDistance, GPSManager.locationListener);
            }
            else {
                    final List<String> providers = GPSManager.locationManager.getProviders(true);
                    for (final String provider : providers)
                    {
                            GPSManager.locationManager.requestLocationUpdates(provider, GPSManager.gpsMinTime,
                                            GPSManager.gpsMinDistance, GPSManager.locationListener);
                    }
            }
    }
    public void stopListening() {
            try
            {
                    if (GPSManager.locationManager != null && GPSManager.locationListener != null) {
                            GPSManager.locationManager.removeUpdates(GPSManager.locationListener);
                    }
                    GPSManager.locationManager = null;
            }
            catch (final Exception ex) {
                ex.printStackTrace();
            }
    }
    public void onGpsStatusChanged(int event) {
        int Satellites = 0;
        int SatellitesInFix = 0;
        if (ActivityCompat.checkSelfPermission(mcontext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mcontext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) mcontext, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
        }
        int timetofix = locationManager.getGpsStatus(null).getTimeToFirstFix();
        Log.i("GPs", "Time to first fix = "+String.valueOf(timetofix));
        for (GpsSatellite sat : locationManager.getGpsStatus(null).getSatellites()) {
            if(sat.usedInFix()) {
                SatellitesInFix++;              
            }
            Satellites++;
        }
        Log.i("GPS", String.valueOf(Satellites) + " Used In Last Fix ("+SatellitesInFix+")"); 
    }
}

GPSCallback.java

public interface GPSCallback{
    public abstract void onGPSUpdate(Location location);
}

最新更新