我正在创建的应用需要使用gps来跟踪用户在练习期间采取的步数。如果我不能计算步数,至少我想计算非常准确的距离。
我的代码可以将地图和用户位置显示为蓝点。它确实跟着我,但有时候是非常不准确的。我可以得到gps来获取当前位置,但它不是很准确。我听说有一个新的位置API,但我找不到示例代码。我无法发现它们之间的区别..
以下有很多代码是不必要的,原因是我决定不包括地图。 我的地图api v2无法在模拟器上显示。 blue-stack显示空白地图,无法输入位置数据。我需要使用与我的团队相同的模拟器。我知道地图不是必需的,但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 :(得分:0)
一旦你有距离,你可以除以人的平均步长,你可以让用户输入参数。
在没有移动的情况下(在步进训练师上)进行锻炼时,这将不起作用。 但是对于在户外散步或跑步,这将起作用。