2012-07-14 119 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