在android中使用GPS确定车辆的速度 [英] Determining the speed of a vehicle using GPS in android

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

问题描述

我想知道如何使用您的手机在坐在车上时使用 GPS 获取车辆的速度.我读过加速度计不是很准确.另一件事是;坐在车上时是否可以访问 GPS?它不会和你在建筑物中的效果一样吗?

I would like to know how to get the speed of a vehicle using your phone while seated in the vehicle using gps. I have read that the accelerometer is not very accurate. Another thing is; will GPS be accessible while seated in a vehicle. Won't it have the same effect as while you are in a building?

这是我尝试过的一些代码,但我改用了 NETWORK PROVIDER.我将感谢您的帮助.谢谢...

Here is some code I have tried but I have used the NETWORK PROVIDER instead.I will appreciate the help. Thanks...

package com.example.speedtest;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
    LocationManager locManager;
    LocationListener li;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        li=new speed();
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, li);
    }
    class speed implements LocationListener{
        @Override
        public void onLocationChanged(Location loc) {
            Float thespeed=loc.getSpeed();
            Toast.makeText(MainActivity.this,String.valueOf(thespeed), Toast.LENGTH_LONG).show();
        }
        @Override
        public void onProviderDisabled(String arg0) {}
        @Override
        public void onProviderEnabled(String arg0) {}
        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

    }
}

推荐答案

GPS 在车辆中工作正常.NETWORK_PROVIDER 设置可能不够准确,无法获得可靠的速度,并且 NETWORK_PROVIDER 中的位置甚至可能不包含速度.您可以使用 location.hasSpeed() 进行检查(location.getSpeed() 将始终返回 0).

GPS works fine in a vehicle. The NETWORK_PROVIDER setting might not be accurate enough to get a reliable speed, and the locations from the NETWORK_PROVIDER may not even contain a speed. You can check that with location.hasSpeed() (location.getSpeed() will always return 0).

如果您发现 location.getSpeed() 不够准确,或者它不稳定(即剧烈波动),那么您可以通过获取几个 GPS 位置之间的平均距离来自己计算速度并除以经过的时间.

If you find that location.getSpeed() isn't accurate enough, or it is unstable (i.e. fluctuates drastically) then you can calculate speed yourself by taking the average distance between a few GPS locations and divide by the time elapsed.

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

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