找出用户的速度在Android中使用GPS(Find out the speed of the Us

2019-06-26 19:46发布

我试图找出用户的速度,同时用户正在与设备移动。 在这里,我也跟着用示例代码的一环。 即: 这里 SpeedDemo。

问题是,通过使用location.getSpeed()方法,我们发现的速度。 所以,我在设备每次改变位置值,但该值location.getspeed()仅返回“0”。 它为什么会发生,甚至改变了位置本身。

可以知道关于这个的任何一个?

Answer 1:

你必须追踪位置更新,而当方法onLocationChanged(Location location)触发,你可以调用location.getSpeed(); 它会给你正确的速度,如果你的手机实际上是移动的。

但是,如果你是在模拟器测试它,并通过仿真控制器发送位置,它总是返回0。

Updated with Example

public class LocationService implements LocationListener {

    LocationService locationService;
    LocationManager locationManager;
    Location lastLocation;
    private final String TAG = "LocationService" ;

    private final String MOCK_LOCAION_PROVIDER = "FAKE_PROVIDER";

    LocationService(Context ctx) {
        LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
        String mocLocationProvider = MOCK_LOCAION_PROVIDER;


        if (locationManager.getProvider(mocLocationProvider) != null) {
            locationManager.removeTestProvider(mocLocationProvider);
        }
        locationManager.addTestProvider(mocLocationProvider, false, false, false, false, true, true, true, 0, 5);

        locationManager.setTestProviderEnabled(mocLocationProvider, true);
        locationManager.requestLocationUpdates(mocLocationProvider, 0, 0,
        this);


        try {

            List<String> data = new ArrayList<String>();
            InputStream is = ctx.getAssets().open("data.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while ((line = reader.readLine()) != null) {

                data.add(line);
            }
            // Log.e(TAG, data.size() + " lines");

            new MockLocationProvider(locationManager, mocLocationProvider, data).start();

        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    class MockLocationProvider extends Thread {

        private List<String> data;
        private LocationManager locationManager;
        private String mocLocationProvider;
        private String LOG_TAG = "MockLocationProvider";

        public MockLocationProvider(LocationManager locationManager, String mocLocationProvider, List<String> data) throws IOException {

            this.locationManager = locationManager;
            this.mocLocationProvider = mocLocationProvider;
            this.data = data;
        }

        @Override
        public void run() {

            for (String str : data) {

                try {

                    Thread.sleep(5000);

                } catch (InterruptedException e) {

                    e.printStackTrace();
                }

                // Set one position
                String[] parts = str.split(",");
                Double latitude = Double.valueOf(parts[0]);
                Double longitude = Double.valueOf(parts[1]);
                float speed = Float.valueOf(parts[2]);
                Location location = new Location(mocLocationProvider);
                location.setLatitude(latitude);
                location.setLongitude(longitude);
                location.setSpeed(speed);
                // location.setAltitude(altitude);

                // Log.d(LOG_TAG, location.toString());

                // set the time in the location. If the time on this location
                // matches the time on the one in the previous set call, it will
                // be
                // ignored
                location.setTime(System.currentTimeMillis());

                locationManager.setTestProviderLocation(mocLocationProvider, location);
            }
        }
    }

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        Log.e(TAG, "onLocationChanged");

        // Get Location Speed Here
        Log.d(TAG, "Speed " +location.getSpeed());



    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        // Log.e(TAG, "onProviderDisabled : "+provider);
    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        // Log.e(TAG, "onProviderEnabled : "+provider);
    }

    public void onStatusChanged(String provider, int status, Bundle arg2) {
        // TODO Auto-generated method stub
        // Log.e(TAG, "onStatusChanged : "+status);
    }
}

上面的代码实际上是位置服务类,当你做出这个类的一个实例,它注册一个假的位置服务(不同于GPS和网络)提供商,并通过输入特定文件的一些假的位置参数。

下面是具有data.txt文件latitude,longitude,speed读取位置这的data.txt文件,并输入假的纬度,经度和速度,以及触发时间来改变位置上面的类也被执行Thread.sleep()调用。

DATA.TXT文件

24.856449265609735,67.04308920288086,1.64
24.856749265609735,67.04408920288086,7.64
24.856949265609735,67.04508920288086,11.64
24.857649265609735,67.04716920288086,13.64
24.857949265609735,67.04736920288086,12.64
24.857949265609735,67.04742520288086,8.64
24.857949265609735,67.04747020288086,4.64
24.856749265609735,67.04408920288086,6.11
24.856949265609735,67.04508920288086,2.12
24.857249265609735,67.04608920288086,1.1
24.856949265609735,67.04508920288086,2.13
24.857249265609735,67.04608920288086,0.6
24.856949265609735,67.04508920288086,1.19
24.857249265609735,67.04608920288086,1.6
24.856949265609735,67.04508920288086,2.12
24.857249265609735,67.04608920288086,1.15
24.857849265609735,67.04729920288086,17.64
24.857949265609735,67.04736920288086,12.64
24.857949265609735,67.04739920288086,16.64
24.857949265609735,67.04742520288086,8.64
24.857949265609735,67.04747020288086,4.64
24.856749265609735,67.04408920288086,6.11
24.856949265609735,67.04508920288086,2.12
24.857249265609735,67.04608920288086,1.1
24.856949265609735,67.04508920288086,2.13
24.857249265609735,67.04608920288086,0.6
24.856949265609735,67.04508920288086,1.19
24.857249265609735,67.04608920288086,1.6
24.856949265609735,67.04508920288086,2.12
24.857249265609735,67.04608920288086,1.15
24.857849265609735,67.04729920288086,17.64
24.857949265609735,67.04736920288086,12.64
24.857949265609735,67.04739920288086,16.64
24.857949265609735,67.04742520288086,8.64
24.857949265609735,67.04747020288086,4.64
24.856749265609735,67.04408920288086,6.11
24.856949265609735,67.04508920288086,2.12
24.857249265609735,67.04608920288086,1.1
24.857849265609735,67.04729920288086,17.64
24.857949265609735,67.04736920288086,12.64
24.857949265609735,67.04739920288086,16.64
24.857949265609735,67.04742520288086,8.64
24.857949265609735,67.04747020288086,4.64
24.856749265609735,67.04408920288086,6.11
24.856949265609735,67.04508920288086,2.12
24.857249265609735,67.04608920288086,1.15
24.856949265609735,67.04508920288086,2.13
24.857249265609735,67.04608920288086,0.6
24.856949265609735,67.04508920288086,1.19
24.857249265609735,67.04608920288086,1.6
24.856949265609735,67.04508920288086,2.12
24.857249265609735,67.04608920288086,1.15
24.856949265609735,67.04508920288086,2.13
24.857249265609735,67.04608920288086,0.6
24.856949265609735,67.04508920288086,1.19
24.857249265609735,67.04608920288086,1.6
24.856949265609735,67.04508920288086,2.12
24.857249265609735,67.04608920288086,1.15


Answer 2:

我认为,如果特定的位置具有可用于GPS速度分量getSpeed()才有效。 否则,它会返回0.0所有的时间。

对于您可以检查使用情况location.hasSpeed()方法。 如果真那么做你的东西别人想的另一种方式。



Answer 3:

首先,我们需要检查是否全球定位系统启用或不使用位置管理器。

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

然后,你需要实现GpsStatus.Listener得到GPS的更新。 它将返回的GPS位置更新。

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) {
                    }
            };

从这个位置,您可以抽到当前速度。

speed = location.getSpeed();

请参考我的网站在这里了解更多详情: http://velmm.com/get-current-user-speed-using-gps-android/



文章来源: Find out the speed of the User in android Using GPS