Android 谷歌地图上的位置错误。无法检索到与下一个活动的正确距离
我的android应用程序可以在地图上追踪用户的位置,并计算他行走或跑步的距离和持续时间。然后,当我按下停止键时,将其传递到下一页。 我的距离和持续时间计算应该是正确的,但位置是错误的。无论我在家里不移动或移动几米,它有时都不会改变或改变很多,导致距离不准确。我如何确保我走路时的准确性,即使是几米,因为当我在场时,我会走很短的距离。 第二,我可以将持续时间数据传递到下一页,但当我传递距离时,它是0.0,但显示的值根本不是0.0(得到的值虽然不准确)。我按下停止按钮时传递信息 我的主要活动Java代码,显示地图,距离,持续时间Android 谷歌地图上的位置错误。无法检索到与下一个活动的正确距离,android,location,google-maps-api-2,Android,Location,Google Maps Api 2,我的android应用程序可以在地图上追踪用户的位置,并计算他行走或跑步的距离和持续时间。然后,当我按下停止键时,将其传递到下一页。 我的距离和持续时间计算应该是正确的,但位置是错误的。无论我在家里不移动或移动几米,它有时都不会改变或改变很多,导致距离不准确。我如何确保我走路时的准确性,即使是几米,因为当我在场时,我会走很短的距离。 第二,我可以将持续时间数据传递到下一页,但当我传递距离时,它是0.0,但显示的值根本不是0.0(得到的值虽然不准确)。我按下停止按钮时传递信息 我的主要活动Java
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.0;
double time = 0.0;
TextView distance;
Button btnDuration;
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;
MyCount counter;
int timer = 0;
@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();
}
distance=(TextView)findViewById(R.id.Distance);
btnDuration=(Button)findViewById(R.id.Duration);
btnStartMove=(Button)findViewById(R.id.Start);//start moving
btnStop=(Button)findViewById(R.id.Stop);
//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;
startDistance=true;
counter= new MyCount(30000,1000);
counter.start();
Toast.makeText(MainActivity.this,
"Pressed Start",
Toast.LENGTH_LONG).show();
}
});
btnStop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
startButtonClicked=false;
startDistance=false;
//Double.valueOf(distance.getText().toString()
Double value=dist;
Double durationValue=time;
Intent intent = new Intent(MainActivity.this, FinishActivity.class);
intent.putExtra("dist", "value");
intent.putExtra("time",durationValue);
startActivity(intent);
finish();
}
});
btnDuration.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
if(startButtonClicked=true)
{
time=n*30+r1;
Toast.makeText(MainActivity.this,"Duration :"+String.valueOf(time),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();
}
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());
//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)
{
distance.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);
}
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;
}
}}
将信息传递到此页面,距离0.0(不准确)持续时间正常
public class FinishActivity extends Activity {
TextView displayDistance;
TextView displayDuration;
TextView displaySports;
TextView userID;
TextView sportID;
TextView caloriesBurned;
Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finish);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
Double value = extras.getDouble("dist");
Double durationValue = extras.getDouble("time");
displayDistance=(TextView)findViewById(R.id.finishDistance);
displayDistance.setText("Distance: " + value);
displayDuration=(TextView)findViewById(R.id.finishDuration);
displayDuration.setText("Duration: " + durationValue + " seconds");
}
back=(Button)findViewById(R.id.Back);
back.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(FinishActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});}
我的地图输出,位置不准确。我搜索了很长时间,但似乎没有解决方案。请帮助。谢谢。每次跑步的起始位置、速度和改变位置的地方似乎都不一样。它似乎超出了我的控制(或者我可以,但我不知道..我是gps的初学者..大多数时候它需要等待一段时间才能显示距离
Gps在屋顶下可能无法正常工作。所以在屋外试试吧 你也有
intent.putExtra("dist", "value")
关键是距离
值是字符串而不是双精度
所以你在传递字符串
但当你得到你得到双倍
Double value = extras.getDouble("dist"); // wrong
如果您传递一个双精度值,那么您可以在下一个活动中获得双精度值。但是您传递字符串,然后尝试获取双精度值,这是错误的
你需要什么
public Intent putExtra (String name, double value)
及
请只发布相关代码。您发布了整个活动代码。您在哪里计算距离?您可以记录dist@
System.out.println的值(“距离是:“+results[0]);dist+=results[0]
并检查值是什么?如何记录值?当我在emulator上测试它时,我认为dist值应该是正确的,但主要是它基于gps检测到的位置,因为gps检测到许多错误的位置,它将是不准确的。尝试它驱逐房子我确实在房子外面尝试过。当我在家时,它会更好我需要一些时间,但大部分环境似乎都是正确的。当我在车里时,位置似乎也更好,但我认为仍有一些不准确之处。但当我现在在一座大楼里。它只是多一点(但不是全部)当我移动得非常快时准确。谢谢。现在可以转移我的距离。但是,当我按back时,活动似乎重新开始,但持续时间仍在累积。但当我按stop时,我调用finish。@Myst restarting是此Intent Intent=newIntent的bcoz(FinishActivity.this,MainActivity.class);startActivity(intent);finish();
只要调用finish,就可以返回到上一个活动,并在导航到时停止在第一个活动中累积持续时间second@Myst从第一个导航到第二个导航时停止计时器。只需调用finish()
当您按后退时,以秒为单位。我的持续时间现在可以工作了。我使用计数器。取消();,n=0;,r1=null,time=0.0;当我按stop时。我不认为我需要所有的,但因为它起作用,我现在专注于我的另一个问题。主要问题是位置完全错了。我不知道如何使它准确。不过,感谢你的建议。我的位置似乎只在我处于护理状态且移动速度很快时更准确一点。但是该应用程序的目的是跟踪步行/跑步速度。当我在场时,我最多只能在房间里行走
public double getDoubleExtra (String name, double defaultValue)