2014-01-28 80 views
0

的應用程序,我創建需要使用GPS來追蹤用戶的步驟已經多少步鍛鍊; Tibial期間拍攝的。如果我至少不能計算這些步驟,我想計算非常準確的距離。android精確gps。計算所採取的步驟

我的代碼可顯示地圖和用戶位置的藍色圓點。它沒有跟着我,但是是相當不準確sometimes.i可以讓GPS功能獲取當前位置,但它是不是很準確。我聽說有一個新的位置API,但我找不到示例代碼。我不能當場的差在它們之間..

以下具有大量的代碼是不必要的原因是我決定不包括地圖。 我的地圖API V2無法顯示在模擬器。藍色堆疊顯示空白地圖,無法輸入位置數據。我需要使用與我的團隊相同的模擬器。我知道地圖不是要求,但GPS需要計算步長/準確距離。

import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.support.v4.app.FragmentActivity; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.view.Menu; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MainActivity extends FragmentActivity implements LocationListener, Runnable,  TextWatcher{ 

protected LocationManager locationManager; 
private GoogleMap googleMap; 
Button btnStartMove,btnPause,btnResume,btnStop; 
//TextView Distance; 
static double n=0; 
    Long s1,r1; 
    double lat1,lon1,lat2,lon2; 
    double dis=0.0; 
    MyCount counter; 
    Thread t1; 
    EditText userNumberInput; 
    boolean bool=false; 
    int count=0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if(isGooglePlay()) 
    { 
     //setContentView(R.layout.activity_main); 
     setUpMapIfNeeded(); 
    } 
    btnStartMove=(Button)findViewById(R.id.Start);//start moving 
    btnPause=(Button)findViewById(R.id.Pause);//pause 
    btnResume=(Button)findViewById(R.id.Resume);//resume 
    btnStop=(Button)findViewById(R.id.Stop); 
    userNumberInput=(EditText)findViewById(R.id.UserInput); 
    userNumberInput.addTextChangedListener(this); 
    //Distance=(TextView)findViewById(R.id.Distance); 
    btnStartMove.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      //start(v); 

      //start(v); 
    } 

    }); 
    btnPause.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      //start(v); 
     } 
    }); 
    btnResume.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      //start(v); 
     } 
    }); 
    btnStop.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      //start(v); 
     } 
    }); 
} 

@Override 
public void afterTextChanged(Editable editable) { 
    try 
    { 
     int no = Integer.parseInt(editable.toString()); 
     if(no >= 100) 
     { 
      editable.replace(0, editable.length(), "99"); 
     } 
    } 
    catch(NumberFormatException e){} 
} 



@Override 
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
     int arg3) { 
    // TODO Auto-generated method stub 

} 



@Override 
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
    // TODO Auto-generated method stub 

} 

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

    //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) { 
    //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!")); 

} 

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

public void start (View v){ 

    switch(v.getId()){ 
    case R.id.Start: 
     Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (location != null) 
     { 
      lat2=location.getLatitude(); 
      lon2=location.getLongitude(); 
      bool=true; 
      t1=new Thread(this); 
      t1.start(); 
      counter= new MyCount(30000,1000); 
      counter.start(); 

     } 
     else 
     { 
      Toast.makeText(MainActivity.this, "null location", 
        Toast.LENGTH_LONG).show(); 
     } 
    break; 
    case R.id.Pause: 
     counter.cancel(); 
     bool=false; 
     break; 
    case R.id.Resume: 
     counter= new MyCount(s1,1000); 
    counter.start(); 
    bool=true; 
    break; 
//  case R.id.Distance: 
//   double time=n*30+r1; 
//   //Distance.setText("Distance:" + dis); 
//   Toast.makeText(MainActivity.this,"distance in metres:"+String.valueOf(dis)+"Velocity in m/sec :"+String.valueOf(dis/time)+"Time :"+String.valueOf(time),Toast.LENGTH_LONG).show(); 
//   Toast.makeText(MainActivity.this, "Value of Count:" + String.valueOf(count), 
//     Toast.LENGTH_LONG).show(); 
//  break;   
    case R.id.Stop: 
     counter.cancel(); 
     counter= new MyCount(30000,1000); 
     bool = false; 

    } 
} 

public class MyCount extends CountDownTimer{ 
    public MyCount(long millisInFuture, long countDownInterval) { 
    super(millisInFuture, countDownInterval); 
    } 
    @Override 
    public void onFinish() { 
     counter= new MyCount(30000,1000); 
    counter.start(); 
    n=n+1; 
    } 
    @Override 
    public void onTick(long millisUntilFinished) { 
     s1=millisUntilFinished; 
     r1=(30000-s1)/1000; 
     //e1.setText(String.valueOf(r1)); 


    } 
} 

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

public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results) 
{ 

} 

@Override 
public void run() 
{ 
    count=6; 
    //LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    lat1=location.getLatitude(); 
    lon1=location.getLongitude(); 
    while(bool==true) 
    { 
     if(lat1!=lat2 || lon1!=lon2) 
     { 
      dis+=getDistance(lat2,lon2,lat1,lon1); 
      lat2=lat1; 
      lon2=lon1; 
      count =7; 
      //Distance.setText("Distance: " + dis + " Count: " + count); 
      count++; 
     } 

    } 

}} 

回答

0

一旦你有距離,你可以除以人平均步長,你可以讓用戶輸入參數。做練習移動內部消除(在一步教練)當

這是行不通的。 但是對於在戶外散步或跑步,這將起作用。