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

时间:2011-11-25 07:25:10

标签: android gps location

我想问你有没有办法计算两个地点之间的速度 例如,如果用户在位置A然后移动到位置B,如何知道这两点之间的用户速度? 此外,我可以确定用户是在走路还是在汽车或火车......等等?

4 个答案:

答案 0 :(得分:6)

  1. 每隔30秒或您喜欢的间隔读取当前位置。
  2. 在两个位置之间取得不同,并使用以下内容转换为里程:
  3. http://www.movable-type.co.uk/scripts/latlong.html

    1. 在此期间通过除以您的时间间隔来获得速度。
    2. 通过速度值,您可以确定速度是走路(最慢),汽车还是火车(最快)。

答案 1 :(得分:1)

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

整条路的平均速度为

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

答案 2 :(得分:0)

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

以获得用户使用此行代码的速度:

speed = loc.getSpeed();

在onLocation更改功能。

答案 3 :(得分:-1)

我将解释如何使用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);
}