-9

如何使用手机传感器或 GPS 计算速度。使用它我想计算距离和其他东西。我想知道一个人的行驶速度。即使手机在口袋里。我该怎么做呢?如果可能,请提供一些示例代码。

4

1 回答 1

0

这是使用 GPS 计算 Android 手机速度的示例...

LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                mlocListener);



public class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {
            loc.getLatitude();
            loc.getLongitude();

            Geocoder gcd = new Geocoder(getApplicationContext(),
                    Locale.getDefault());
            try {
                mAddresses = gcd.getFromLocation(loc.getLatitude(),
                        loc.getLongitude(), 1);

            } catch (IOException e) {

            }

            String cityName = (mAddresses != null) ? mAddresses.get(0)
                    .getLocality() : TimeZone.getDefault().getID();
            String countryName = (mAddresses != null) ? mAddresses.get(0)
                    .getCountryName() : Locale.getDefault().getDisplayCountry()
                    .toString();


            mCurrentSpeed.setText(loc.getSpeed());
        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Disabled",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Enabled",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
于 2012-07-14T08:51:01.330 回答