2017-04-21 20 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提供商比网络更准确吗? –