2017-04-21 62 views
0

我已經創建了一個簡單的程序來計算我的位置,並希望計算應用程序開啓時用戶的行駛速度和總距離。 該位置似乎運作良好,但當我試圖計算速度和距離時,它顯示我一些瘋狂的數字,而我的手機沒有移動,連接到電腦。那裏有什麼問題?Android瘋狂的速度和距離旅行

這裏是我的代碼:

public class MapsActivity extends AppCompatActivity { 

LocationManager locationManager; 
TextView tvSpeed; 
TextView tvLong; 
TextView tvLati; 
TextView tvDistance; 
double oldLat; 
double oldLong; 
double startTime; 
double endTime; 
double overalDistance; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 

    tvSpeed = (TextView) findViewById(R.id.textViewSpeed); 
    tvLong = (TextView) findViewById(R.id.textViewLong); 
    tvLati = (TextView) findViewById(R.id.textViewLati); 
    tvDistance = (TextView) findViewById(R.id.textViewDistance); 

    oldLat = 0; 
    oldLong = 0; 
    overalDistance = 0; 
    startTime= SystemClock.elapsedRealtime(); 



    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     return; 
    } 
    if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       double latitude = location.getLatitude(); 
       double longitude = location.getLongitude(); 
       endTime = SystemClock.elapsedRealtime(); 
       tvLati.setText("Latitude: " + Double.toString(latitude)); 
       tvLati.invalidate(); 
       tvLong.setText("Longitude: " + Double.toString(longitude)); 
       tvLong.invalidate(); 
       double distance = getDistance(oldLat,oldLong,latitude,longitude); 
       double time = (endTime - startTime)/1000; 
       startTime=endTime; 
       oldLat = latitude; 
       oldLong = longitude; 
       tvSpeed.setText("Speed: " + Double.toString(distance/time)); 
       tvSpeed.invalidate(); 
       overalDistance += distance; 
       tvDistance.setText("Overall Distance: " + Double.toString(overalDistance)); 
       tvDistance.invalidate(); 
      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 

      } 
     }); 
    } 
    else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       double latitude = location.getLatitude(); 
       double longitude = location.getLongitude(); 
       endTime = SystemClock.elapsedRealtime(); 
       tvLati.setText("Latitude: " + Double.toString(latitude)); 
       tvLati.invalidate(); 
       tvLong.setText("Longitude: " + Double.toString(longitude)); 
       tvLong.invalidate(); 
       double distance = getDistance(oldLat,oldLong,latitude,longitude); 
       double time = (endTime - startTime)/1000; 
       startTime=endTime; 
       oldLat = latitude; 
       oldLong = longitude; 
       tvSpeed.setText("Speed: " + Double.toString(distance/time)); 
       tvSpeed.invalidate(); 
       overalDistance += distance; 
       tvDistance.setText("Overall Distance: " + Double.toString(overalDistance)); 
       tvDistance.invalidate(); 
      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 

      } 
     }); 

    } 
} 

public double getDistance(double lat1, double lon1, double lat2, double lon2) 
{ 
    double latA = Math.toRadians(lat1); 
    double lonA = Math.toRadians(lon1); 
    double latB = Math.toRadians(lat2); 
    double lonB = Math.toRadians(lon2); 
    double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) + 
      (Math.sin(latA) * Math.sin(latB)); 
    double ang = Math.acos(cosAng); 
    double dist = ang *6371; 
    return dist; 
} 

,這裏是我的手機上的輸出 Output

回答

0

您初始化oldLatoldLong變量爲0。於是,第一時間,你的應用程序中有一個位置,它會計算當前位置和(0,0)座標之間的距離。

您必須實現一個if語句不計算距離第一次:

@Override 
public void onLocationChanged(Location location) { 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 

    tvLati.setText("Latitude: " + Double.toString(latitude)); 
    tvLati.invalidate(); 
    tvLong.setText("Longitude: " + Double.toString(longitude)); 
    tvLong.invalidate(); 

    if(oldLat == 0 && oldLong == 0) { 
     oldLat = latitude; 
     oldLong = longitude; 
     return; 
    } 

    endTime = SystemClock.elapsedRealtime(); 
    double distance = getDistance(oldLat,oldLong,latitude,longitude); 
    double time = (endTime - startTime)/1000; 
    startTime=endTime; 
    oldLat = latitude; 
    oldLong = longitude; 
    tvSpeed.setText("Speed: " + Double.toString(distance/time)); 
    tvSpeed.invalidate(); 
    overalDistance += distance; 
    tvDistance.setText("Overall Distance: " + Double.toString(overalDistance)); 
    tvDistance.invalidate(); 
} 
+0

謝謝!它非常明顯。但是現在,當我測試這個時,它很不準確。是因爲我正在使用android.location。*而我應該google.gms.location?看來,如果我想改變這一點,我不得不重寫整個項目。 –

+0

我對我的項目使用android.location。*,它非常穩定。但也許不使用網絡提供商,它比GPS提供商更不準確(但需要時間才能找到位置)。順便說一下,如果我的回答糾正了你的問題,謝謝接受。 – Guillaume

+0

我已經添加了TextView來顯示兩次之間的時間,我的程序進入「onLocationChange」,而我開車的時間是19到22秒。這使得本地化非常不準確。 GPS提供商比網絡更準確嗎? –