奇怪的位置,使用gps的地图的距离输出

时间:2014-02-06 14:03:13

标签: android gps location distance

在我运行我的应用程序的一段时间后,距离保持为0这是正确的,因为我没有移动,但是在20-30分钟之间它增加到8-18米,我忘了。更进一步,它增长到百。随着时间的推移,标记变得更加分散 我如何确定一个位置,如果我从不移动,停止改变? 如果我搬家,从我的房间到我的起居室,应该是4-7米,我想,距离永远不会增加 对于那些之前做过类似事情的人来说,蓝点和红色标记是否在一起。我的蓝点在大多数时候都不正确

我真的不知道我可以在哪里改进我的代码或输出的原因是什么..

enter image description here

我的主要活动文件

public class MainActivity extends FragmentActivity implements LocationListener{

protected LocationManager locationManager;
private GoogleMap googleMap;
Button btnStartMove,btnPause,btnResume,btnStop;
static double n=0;
Long s1,r1;
double dis=0.0;
Thread t1;
EditText userNumberInput;
boolean bool=false;
int count=0;

double speed = 1.6;
double lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4;
double dist = 0;
TextView distanceText;
float[] result;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES =1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 4000; //in milliseconds
boolean startDistance = false;
boolean startButtonClicked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, this);
    if(isGooglePlay())
    {
        setUpMapIfNeeded();
    }
    distanceText=(TextView)findViewById(R.id.Distance);
    btnStartMove=(Button)findViewById(R.id.Start);//start moving

    //prepare distance...........
    Log.d("GPS Enabled", "GPS Enabled");  
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, true);
    Location location=locationManager.getLastKnownLocation(provider);

    btnStartMove.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) {

            Log.d("GPS Enabled", "GPS Enabled");  
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            String provider = locationManager.getBestProvider(criteria, true);
            Location location=locationManager.getLastKnownLocation(provider);
            lat3 = location.getLatitude();
            lon3 = location.getLongitude();
            startButtonClicked=true;
            Toast.makeText(MainActivity.this,
                      "start is true",    
                      Toast.LENGTH_LONG).show();
    }


    });

    if(location!= null)
    {
        //Display current location in Toast
        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(MainActivity.this, message,
                Toast.LENGTH_LONG).show();

        //Display current location in textview  
        //latitude.setText("Current Latitude: " + String.valueOf(location.getLatitude())); 
        //longitude.setText("Current Longitude: " + String.valueOf(location.getLongitude()));
    }
    else if(location == null)
    {
        Toast.makeText(MainActivity.this,
                "Location is null",    
                Toast.LENGTH_LONG).show();
    }


}

private void setUpMapIfNeeded() {

    if(googleMap == null)
    {
        Toast.makeText(MainActivity.this, "Getting map",
                Toast.LENGTH_LONG).show();
        googleMap =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.displayMap)).getMap();

        if(googleMap != null)
        {
            setUpMap();
        }
    }

}

private void setUpMap() 
{
    //Enable MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);

    //Get locationManager object from System Service LOCATION_SERVICE
    //LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    //Create a criteria object to retrieve provider
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    //Get the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);
    if(provider == null)
    {
        onProviderDisabled(provider);
    }
    //set map type
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    //Get current location
    Location myLocation = locationManager.getLastKnownLocation(provider);
    if(myLocation != null)
    {
        onLocationChanged(myLocation);
    }       
    locationManager.requestLocationUpdates(provider, 0, 0, this);
}

private boolean isGooglePlay() 
{
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (status == ConnectionResult.SUCCESS)
    {

        Toast.makeText(MainActivity.this, "Google Play Services is available",
                Toast.LENGTH_LONG).show();
        return(true);
    }
    else
    {
            GooglePlayServicesUtil.getErrorDialog(status, this, 10).show();

    }
    return (false);

 }

@Override
public void onLocationChanged(Location myLocation) {
    System.out.println("speed " + myLocation.getSpeed());


//      if(myLocation.getSpeed() > speed)//return 0 in actual
//      {
        //show location on map.................
        //Get latitude of the current location
        double latitude = myLocation.getLatitude();
        //Get longitude of the current location
        double longitude = myLocation.getLongitude();
        //Create a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);
        //Show the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        //Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));

        //show distance............................

        if(startDistance == true)
        {

            Toast.makeText(MainActivity.this,
                      "Location has changed",    
                      Toast.LENGTH_LONG).show();
                if(myLocation != null)
                {
                    //latitude.setText("Current Latitude: " + String.valueOf(loc2.getLatitude())); 
                    //longitude.setText("Current Longitude: " + String.valueOf(loc2.getLongitude()));
                    float[] results = new float[1]; 
                    Location.distanceBetween(lat3, lon3, myLocation.getLatitude(), myLocation.getLongitude(), results);
                    System.out.println("Distance is: " + results[0]);               

                    dist += results[0];            
                    DecimalFormat df = new DecimalFormat("#.##"); // adjust this as appropriate
                if(count==1)
                {
                    distanceText.setText(df.format(dist) + "meters");
                }
                    lat3=myLocation.getLatitude();
                    lon3=myLocation.getLongitude();
                    count=1;
              }

        }
        if(startButtonClicked == true)
        {
            startDistance=true;
        }

    //}


}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(MainActivity.this,
            "Provider disabled by the user. GPS turned off",
            Toast.LENGTH_LONG).show();
}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(MainActivity.this,
            "Provider enabled by the user. GPS turned on",
            Toast.LENGTH_LONG).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Toast.makeText(MainActivity.this, "Provider status changed",
            Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, this);
}}

我尝试将最小时间和距离更改为各种数据并保持不变。我从来没有真正移动我的手机大部分时间在上面的输出。有一次,我暂时离开,但距离没有增加。对于那些完成类似运行的应用程序的人,我需要更改哪一部分......我无法发现哪里出错了......

1 个答案:

答案 0 :(得分:0)

问题是,只要您不使用GPS,您的位置就会非常不准确,因为它只使用互联网和您的移动网络信息。这种测量的准确度可以变化很大,从10-20米到2000-3000 +米。也就是说,即使停留在同一个网络和相同的位置,有时精度也会随之变化,并随之改变您的位置。因此,您现在可能会看到准确的位置,几分钟后您可能距离地图上的一个地方一公里。如果您正在使用GPS,请记住GPS并非始终可用(即,当您在室内时)并且需要一些时间才能启动。

您可能只想使用GPS,这会使您的应用无法使用,以防您不在外面,或者您可以提出算法来检查您是否真的在新位置(使用位置准确度,自上次更新位置以来的时间,或类似的事情。)