Reputation: 109
Trying to create a running gps in android, using this code to calculate the distance between two points every second (at least that's what I think it's doing):
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
// check if GPS enabled
if (gps.canGetLocation()) {
final Handler h = new Handler();
final int delay = 1000; //milliseconds
h.postDelayed(new Runnable() {
public void run() {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Location locationA = new Location("point A");
final Location locationB = new Location("point B");
locationA.setLatitude(latitude);
locationA.setLongitude(longitude);
Timer t = new Timer();
h.postDelayed(new Runnable() {
public void run() {
double latitude1 = gps.getLatitude();
double longitude2 = gps.getLongitude();
locationB.setLatitude(latitude1);
locationB.setLongitude(longitude2);
}
}, delay);
float distance = locationA.distanceTo(locationB);
finalDistance[0] = (finalDistance[0] + distance);
displayDistance.setText(String.valueOf(finalDistance[0]));
h.postDelayed(this, delay);
}
}, delay);
The distance changes more or less by the same increment whether I'm walking or not walking.
The distance I get is also a weird value, e.g.: 6.47875890357E9
My questions: 1)What unit is this distance in?
2)Am I getting some random gobbledigook because of crap programming skills?
Upvotes: 0
Views: 107
Reputation: 5789
1) The units are meters (see distanceTo()).
2) Yes. But like all programmers, you're still learning, so to be more helpful I won't just leave it there! The reason you are getting a huge distance is because your code is getting the distance between locationA
and locationB
, and you haven't set locationB
at the time you execute the line:
float distance = locationA.distanceTo(locationB);
So you're getting the distance between locationA
and lat/long 0,0.
You're only setting the latitude and longitude of locationB
in your Runnable
which will be executed after distance
has been set. So, you probably need to move your distance calculation and the code that outputs it to within your Runnable
that sets the latitude and longitude for locationB
. You'll also have to make locationA
final
to do that.
Upvotes: 1