• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps distanceTo() Android incorrect reading

GaryDoo

Newbie
Hi!! :)

I am trying to get the distance between two points, but the result i'm getting is incorrect.

My problem is I am using a real device and an emulator. So I'm getting the coordinates of my device and am sending dummy coordinates to my emulator for testing. My problem is that I get 9. something no matter what dummy coordinates I send.

I send Client A's coordinates in a string format as below:
[HIGH]coordinates = (latitude + "," + longitude);[/HIGH]

I then split that string, and parse for doubles as below:
[HIGH]String[] Loc = location.split(",");
Clatitude = Double.parseDouble(Loc[0]);
Clongitude = Double.parseDouble(Loc[1]);[/HIGH]

Below I try to get the distance between the two points:
[HIGH]public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Location parent = new Location("parent");

Platitude = location.getLatitude();
Plongitude = location.getLongitude();

parent.setLatitude(Platitude / 1e6);
parent.setLongitude(Plongitude / 1e6);

Location child = new Location("child");

child.setLatitude(Clatitude / 1e6);
child.setLongitude(Clongitude / 1e6);

distance = parent.distanceTo(child);

String distanceSt = String.valueOf(distance);

TextView myTextview = (TextView) findViewById(R.id.distTo);
myTextview.setText(distanceSt);
}
}[/HIGH]

I'm not sure if it's an issue with my code or if it's an issue with the way I'm trying to display the distance?

Any help on this would be greatly appreciated, I'm not too experienced, I teaching myself with the help of these forums.

Regards,
Gary

*****EDIT*****

after further testing I can rule out problems with the split string with the following code:
[HIGH]Clatitude = Double.valueOf(currLocation[0]);
clat = String.valueOf(Clatitude);

Clongitude = Double.valueOf(currLocation[1]);
clong = String.valueOf(Clongitude);[/HIGH]

I then displayed them in a text view for testing, and they're both correct
[HIGH]TextView myTextview = (TextView) findViewById(R.id.dist);
myTextview.setText(clong);[/HIGH]

So now that I've established that it's not the split or the parsing that is the problem, would anyone have any other ideas? Could it be on the end of the emulator?
 
_________________
edit:
sorry my mistake.... parsed in the wrong direction...
_________________

Just curious....
why dont you use an object for the coordinates? all that parsing costs a lot of time... something like:

[HIGH]public class Coordinates {

private double x;
private double y;

public Coordinates(double x, double y) {
this.x = x;
this.y = y;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

public void setX(double x) {
this.x = x;
}

public void setY(double y) {
this.y = y;
}

}[/HIGH]
 
Hi,

thanks for your reply

the variable distance is float, I have it set like so

[HIGH]String distanceSt = String.valueOf(distance);[/HIGH]

I order to allow me to display it to a textview.

The coordinates are sent from Client A as a string to the server, which then passes them onto Client B, it was my thinking that these then need to be split then parsed in order to use them as inputs into the distanceTo() method? :confused:
 
Back
Top Bottom