2012-07-14 123 views
-9

如何使用手机传感器或GPS计算速度。使用我想要计算距离和其他东西。我想找出一个人旅行的速度。即使手机在口袋里。我该怎么做呢?请尽可能提供一些示例代码。在Android手机上计算速度

+0

StackOverflow.com是*不*免费的编码服务。你有什么尝试? – thkala 2012-07-14 20:47:23

+0

我试图制作一个应用程序,它可以计算出一个人在跑步或慢跑时以何种速度出行。 – Gaurav 2012-07-22 09:18:25

回答

0

这是例如为了计算你的Android手机使用GPS的速度...

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

谢谢:)我一定会试试:) – Gaurav 2012-07-22 09:08:05