Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Distance function help

Hey guys,
so I have Location class, and a radio class(the radio class contains the coordinates). Basically for a program I am writing I need the location to take the coordinates from the radio class, and use the distance formula for the 2 sets of coordinates.
Location:
```
public class Location {

private double lat, lon;

public Location (double lat, double lon){
this.lat=lat;
this.lon=lon;
}

public Location (){
this.lat=0.0;
this.lon=0.0;
}

public void setLat(double lat){
this.lat=lat;
}

public void setLon(double lon){
this.lon=lon;
}

public double Distance (double lat1, double lon1, double lat2, double lon2) {

double cosLat2 = Math.cos(lat2);
double sinLonChange = Math.sin(lon2-lon1);
double cosLat1 = Math.cos(lat1);
double sinLat2 = Math.sin(lat2);
double sinLat1 = Math.sin(lat1);
double cosLonChange = Math.cos(lon2-lon1);

double a = Math.pow((cosLat2*sinLonChange), 2);
double b = Math.pow(((cosLat1*sinLat2)-(sinLat1*cosLat2*cosLonChange)), 2);
double c = (sinLat1*sinLat2)+(cosLat1*cosLat2*cosLonChange);

double spherDistance = Math.atan(Math.sqrt(a+b)/c);

double Distance = 3959 * spherDistance;

return Distance;

}

public double getLat(){
return lat;
}

public double getLon(){
return lon;
}
public double getDistance(){
return Distance;
}

public String toString(){
String dist="lat"+lat+"lon"+lon;
return dist;
}

}```

```
public static void main(String[] args) {
Location loc1=new Location(10,20);
Location loc2=new Location(30,40);

System.out.println(loc1.toString());
System.out.println(loc2.toString());

}

}```

Any help or suggestions would be greatly appreciated.
Thank y'all very much!

2. ## Re: Distance function help

Originally Posted by abf617
... I need..to take the coordinates from the radio class, and use the distance formula for the 2 sets of coordinates...
Your Distance function has nothing to do with the Location class, so could be made a static function of any class. (It doesn't make sense for a Location object to have a Distance data member, and yours does not so your Location class has a couple of problems that could be fixed if you really need it.)

I mean, it might make sense to for the Location class to have a Distance function that tales another Location object and calculates distance from the current object, or a Distance function that takes latitude and longitude of somewhere else and calculates distance from the current object. Stuff like that.

In the meanwhile, since the title that you gave the thread indicates to me that you need the Distance function to work, I think it already works. Just call it with latitudes and longitudes (in decimal degrees) for two places on Earth:

```public class Z
{
public static void main(String [] args)
{
// Example at [url]http://en.wikipedia.org/wiki/Great-circle_distance[/url]
// gives results in km.  Your Distance function is set up for statute miles.
// Distance is calculated from an airport in Nashville, Tennessee to
// Los Angeles, California.

double d = DistanceInStatuteMiles(36.12, -86.67, 33.94, -118.4);
System.out.printf("d = %.2f statute miles.\n", d);
} // End of main function

static double DistanceInStatuteMiles(double lat1, double lon1, double lat2, double lon2)
{

double cosLat2 = Math.cos(lat2);
double sinLonChange = Math.sin(lon2-lon1);
double cosLat1 = Math.cos(lat1);
double sinLat2 = Math.sin(lat2);
double sinLat1 = Math.sin(lat1);
double cosLonChange = Math.cos(lon2-lon1);

double a = Math.pow((cosLat2*sinLonChange), 2);
double b = Math.pow(((cosLat1*sinLat2)-(sinLat1*cosLat2*cosLonChange)), 2);
double c = (sinLat1*sinLat2)+(cosLat1*cosLat2*cosLonChange);

double spherDistance = Math.atan(Math.sqrt(a+b)/c);

// Statute miles
double distance = 3959 * spherDistance;
// Change the constant to 6372.8 to get km. (Change the function name too.)

return distance;
} // End of Distance function

} // End of class definition```

Output

d = 1793.66 statute miles.

Now if you have problems that you don't understand with the other classes, you can probably get help if you ask specific questions: Tell us what compiler messages and/or program output that you got and tell us what you don't understand.

Cheers!

Z