找出用户的速度在Android中使用GPS [英] Find out the speed of the User in android Using GPS

查看:227
本文介绍了找出用户的速度在Android中使用GPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出用户的速度,同时用户在使用移动设备。在这里,我也跟着有样code的一个环节。即:这里的Speed​​Demo。

I tried to find out the speed of User, while user is moving with device. Here, I followed one link with sample code. i.e: Here SpeedDemo.

现在的问题是,通过使用 location.getSpeed​​()方法,我们发现速度。所以,我在设备中更改的位置值,但每次 location.getspeed的值()返回'0'而已。它为什么会发生,即使改变了位置本身。

The problem is, by using location.getSpeed() method, we are finding the speed. So, for that I changed the location values in device, but every time the value of the location.getspeed() return '0' only. Why it happen, even changing the location itself.

任何人可以知道这一点?

Can any one known about this?

推荐答案

您必须跟踪的位置更新,而当方法 onLocationChanged(地点位置)触发器,可以叫 location.getSpeed​​(); ,它会给你正确的速度,如果你的手机实际上是移动

You have to track location Updates, and when the method onLocationChanged(Location location) triggers, you can call location.getSpeed(); it will give you correct speed, if your phone is actually moving.

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

But if you are testing it on Simulator, and sending location by emulator controller, it will always return 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);
    }
}

以上code其实是一种位置服务类,当你做出这个类的一个实例,它注册一个假的位置服务(除GPS外,和网络)提供商,并输入一些假的位置参数由给定文件。

The Above code is actually a location service class, when you make an instance of this class, it register a fake Location Service(other than GPS, and Network) provider, and input some fake location parameters by a given file.

下面是具有data.txt文件经度,纬度,速度阅读本data.txt中文件并输入假的纬度,经度和速度上面的类位置,以及触发时间来改变位置也由实现视频下载()电话。

Below is the data.txt file which has latitude,longitude,speed the above class read this data.txt file and input fake lat,lon, and speed in the location, as well as trigger time to change location is also implemented by Thread.sleep() call.

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

这篇关于找出用户的速度在Android中使用GPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆