2017-04-21 4 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

あなたは、だから、0にアプリケーションが持っている最初の時間をあなたのoldLatoldLong変数を初期化します現在の位置と(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"と入力した2倍の時間と、車を運転している間に19〜22秒の時間を表示しました。そのため、ローカリゼーションがかなり不正確になります。 GPSプロバイダがネットワークよりも正確ですか? –