[在Android中使用GPS确定车辆的速度

问题描述 投票:23回答:4

我想知道如何在使用gps坐车的情况下使用手机获得车辆的速度。我已经读到加速度计不是很准确。另一件事是;坐在车辆中时,GPS可以访问。它会不会与您在建筑物中时产生相同的效果?

这里是我尝试过的一些代码,但是我改用了网络供应商。我将感谢您的帮助。谢谢...

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

    }
}
android gps location android-location
4个回答
27
投票

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

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


27
投票

for more information onCalculate Speed from GPS Location Change in Android Mobile Device view this link

主要有两种方法可以通过手机计算速度。

  1. 通过加速度计计算速度
  2. 通过GPS技术计算速度

与GPS Technology的加速度计不同,如果要计算速度,必须启用数据连接和GPS连接。

在这里,我们将使用GPS连接来计算速度。在这种方法中,我们使用GPS定位点在单个时间段内变化的频率。然后,如果我们具有地理位置点之间的真实距离,便可以得到速度。因为我们有距离和时间。速度=距离/时间但是获取两个位置点之间的距离并非易事。因为世界是形状的目标,所以两个地理位置之间的距离因位置和角度而异。所以我们必须使用“Haversine Algorithm”

enter image description here

首先,我们必须授予清单文件中获取位置数据的权限

制作GUIenter image description here

enter image description here

enter image description here

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtCurrentSpeed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="000.0 miles/hour"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <CheckBox android:id="@+id/chkMetricUnits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Use metric units?"/>

然后创建一个接口以获取速度

package com.isuru.speedometer;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

public interface IBaseGpsListener extends LocationListener, GpsStatus.Listener {

      public void onLocationChanged(Location location);

      public void onProviderDisabled(String provider);

      public void onProviderEnabled(String provider);

      public void onStatusChanged(String provider, int status, Bundle extras);

      public void onGpsStatusChanged(int event);

}

执行逻辑以使用GPS位置获得速度

import android.location.Location;

public class CLocation extends Location {

      private boolean bUseMetricUnits = false;

      public CLocation(Location location)
      {
            this(location, true);
      }

      public CLocation(Location location, boolean bUseMetricUnits) {
            // TODO Auto-generated constructor stub
            super(location);
            this.bUseMetricUnits = bUseMetricUnits;
      }


      public boolean getUseMetricUnits()
      {
            return this.bUseMetricUnits;
      }

      public void setUseMetricunits(boolean bUseMetricUntis)
      {
            this.bUseMetricUnits = bUseMetricUntis;
      }

      @Override
      public float distanceTo(Location dest) {
            // TODO Auto-generated method stub
            float nDistance = super.distanceTo(dest);
            if(!this.getUseMetricUnits())
            {
                  //Convert meters to feet
                  nDistance = nDistance * 3.28083989501312f;
            }
            return nDistance;
      }

      @Override
      public float getAccuracy() {
            // TODO Auto-generated method stub
            float nAccuracy = super.getAccuracy();
            if(!this.getUseMetricUnits())
            {
                  //Convert meters to feet
                  nAccuracy = nAccuracy * 3.28083989501312f;
            }
            return nAccuracy;
      }

      @Override
      public double getAltitude() {
            // TODO Auto-generated method stub
            double nAltitude = super.getAltitude();
            if(!this.getUseMetricUnits())
            {
                  //Convert meters to feet
                  nAltitude = nAltitude * 3.28083989501312d;
            }
            return nAltitude;
      }

      @Override
      public float getSpeed() {
            // TODO Auto-generated method stub
            float nSpeed = super.getSpeed() * 3.6f;
            if(!this.getUseMetricUnits())
            {
                  //Convert meters/second to miles/hour
                  nSpeed = nSpeed * 2.2369362920544f/3.6f;
            }
            return nSpeed;
      }



}

GUI的组合逻辑

import java.util.Formatter;
import java.util.Locale;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements IBaseGpsListener {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            this.updateSpeed(null);

            CheckBox chkUseMetricUntis = (CheckBox) this.findViewById(R.id.chkMetricUnits);
            chkUseMetricUntis.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                  @Override
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub
                        MainActivity.this.updateSpeed(null);
                  }
            });
      }

      public void finish()
      {
            super.finish();
            System.exit(0);
      }

      private void updateSpeed(CLocation location) {
            // TODO Auto-generated method stub
            float nCurrentSpeed = 0;

            if(location != null)
            {
                  location.setUseMetricunits(this.useMetricUnits());
                  nCurrentSpeed = location.getSpeed();
            }

            Formatter fmt = new Formatter(new StringBuilder());
            fmt.format(Locale.US, "%5.1f", nCurrentSpeed);
            String strCurrentSpeed = fmt.toString();
            strCurrentSpeed = strCurrentSpeed.replace(' ', '0');

            String strUnits = "miles/hour";
            if(this.useMetricUnits())
            {
                  strUnits = "meters/second";
            }

            TextView txtCurrentSpeed = (TextView) this.findViewById(R.id.txtCurrentSpeed);
            txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits);
      }

      private boolean useMetricUnits() {
            // TODO Auto-generated method stub
            CheckBox chkUseMetricUnits = (CheckBox) this.findViewById(R.id.chkMetricUnits);
            return chkUseMetricUnits.isChecked();
      }

      @Override
      public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if(location != null)
            {
                  CLocation myLocation = new CLocation(location, this.useMetricUnits());
                  this.updateSpeed(myLocation);
            }
      }

      @Override
      public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

      }

      @Override
      public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

      }

      @Override
      public void onGpsStatusChanged(int event) {
            // TODO Auto-generated method stub

      }



}

如果要将米/秒转换为kmph-1,则需要从3.6乘以米/秒答案

从kmph-1起的速度= 3.6 *(从ms-1起的速度)


3
投票
public class MainActivity extends Activity implements LocationListener {

在活动旁边添加工具LocationListener

LocationManager lm =(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        this.onLocationChanged(null);

LocationManager.GPS_PROVIDER, 0, 0,第一个零代表minTime,第二个零代表minDistance,在其中更新值。零表示基本上是即时更新,这可能会延长电池寿命,因此您可能需要对其进行调整。

     @Override
    public void onLocationChanged(Location location) {

    if (location==null){
         // if you can't get speed because reasons :)
        yourTextView.setText("00 km/h");
    }
    else{
        //int speed=(int) ((location.getSpeed()) is the standard which returns meters per second. In this example i converted it to kilometers per hour

        int speed=(int) ((location.getSpeed()*3600)/1000);

        yourTextView.setText(speed+" km/h");
    }
}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}


@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}


@Override
public void onProviderDisabled(String provider) {


}

不要忘记权限

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

1
投票

我们可以使用location.getSpeed();

  try {
                // Get the location manager
                double lat;
                double lon;
                double speed = 0;
                LocationManager locationManager = (LocationManager)
                        getActivity().getSystemService(LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                String bestProvider = locationManager.getBestProvider(criteria, false);
                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                Location location = locationManager.getLastKnownLocation(bestProvider);
                try {
                    lat = location.getLatitude();
                    lon = location.getLongitude();
                    speed =location.getSpeed();
                } catch (NullPointerException e) {
                    lat = -1.0;
                    lon = -1.0;
                }

                mTxt_lat.setText("" + lat);
                mTxt_speed.setText("" + speed);

            }catch (Exception ex){
                ex.printStackTrace();
            }
© www.soinside.com 2019 - 2024. All rights reserved.