8

我试图找出用户的速度,而用户正在移动设备。在这里,我关注了一个带有示例代码的链接。即:这里是SpeedDemo。

问题是,通过使用location.getSpeed()方法,我们正在寻找速度。因此,为此我更改了设备中的位置值,但每次location.getspeed()仅返回“0”的值。为什么会发生,甚至更改位置本身。

任何人都可以知道这件事吗?

4

3 回答 3

8

您必须跟踪位置更新,并且当该方法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()调用实现的。

数据.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
于 2012-08-07T10:21:52.793 回答
1

我认为 getSpeed() 只有在特定位置具有 GPS 可用的速度组件时才会起作用。否则它将一直返回 0.0。

为此,您可以使用条件 location.hasSpeed() 方法进行检查。如果是真的,那么做你的其他事情以另一种方式思考。

于 2013-07-08T09:38:33.443 回答
-1

首先,我们需要使用位置管理器检查是否启用了 Gps。

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/

于 2018-01-25T08:29:49.170 回答