2014-02-06 23 views
0

在我運行我的應用程序一段時間後,距離保持爲0,這是正確的,因爲我沒有移動,但是在20-30分鐘之後它增加到8-18米,我忘了。甚至更晚,它增長到了一百。隨着時間的推移,標記變得更分散。
我怎麼能找到一個位置,如果我從不移動,停止改變? 如果我從我的房間移動到我的客廳,應該是4-7米左右,我認爲距離永遠不會增加。
對於那些以前做過類似事情的人來說,藍點和紅色標記是否合在一起。我的藍點是不正確的大部分時間奇怪的位置,使用gps的地圖的距離輸出

我真的不知道在哪裏可以提高我的代碼或的輸出是什麼原因..

enter image description here

我的MainActivity文件

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); 
}} 

我試着改變各種數據的最短時間和距離,它仍然是一樣的。在上面的輸出中,我絕大部分時間都沒有真正移動手機。有一次,我搬了一會兒,但距離沒有增加。對於那些做類似的運行應用程序,哪部分我需要改變..我不能發現哪裏是錯誤的..

回答

0

事情是,只要你不使用GPS你的位置可以是非常因爲它只使用互聯網和移動網絡信息而不準確。這種測量的準確度可能會有很大的不同,從10-20米到2000-3000米不等。也就是說,即使停留在同一個網絡上,並且在同一地點,有時準確度也會發生變化,而且您的位置也會發生變化。因此,您現在可能會看到您的準確位置,幾分鐘後,您可能距離地圖上的真實位置一公里。如果您使用的是GPS,請記住,GPS並不總是可用的(即當您在室內時)並需要一些時間啓動。

您可能只想使用GPS,如果您不在戶外,您的應用將無法使用,或者您可以想出一個算法來檢查您是否確實處於新位置(使用位置準確性,自上次位置更新以來的時間,或類似的事情)。

+0

我正在使用gps。我檢查我的gps是否可以隨時使用。有時它停留在我的位置。即使當我在室內,當GPS啓用它可能有一些不準確的?事情是,我希望它能夠計算我的距離,即使當我在一個大房間裏走動時,爲了表達目的,但似乎要麼等待一段時間來計算,當我不移動,或者甚至當我移動時,它沒有顯示任何改變。所以沒有辦法讓它在室內更精確?謝謝 – Myst