add GeoPoint.distance()

- return double in distanceTo()
This commit is contained in:
Hannes Janetzek 2014-06-09 00:00:39 +02:00
parent 9b719d857e
commit 2c6a85ee6d
2 changed files with 19 additions and 12 deletions

View File

@ -135,9 +135,9 @@ public class GeoPoint implements Comparable<GeoPoint> {
@Override @Override
public String toString() { public String toString() {
return new StringBuilder() return new StringBuilder()
.append("GeoPoint [lat=") .append("[lat=")
.append(this.getLatitude()) .append(this.getLatitude())
.append(", lon=") .append(",lon=")
.append(this.getLongitude()) .append(this.getLongitude())
.append("]") .append("]")
.toString(); .toString();
@ -170,12 +170,19 @@ public class GeoPoint implements Comparable<GeoPoint> {
* ... * ...
* @return distance in meters * @return distance in meters
*/ */
public int distanceTo(final GeoPoint other) { public double distanceTo(GeoPoint other) {
return distance(latitudeE6 / 1E6,
longitudeE6 / 1E6,
other.latitudeE6 / 1E6,
other.longitudeE6 / 1E6);
}
double a1 = DEG2RAD * latitudeE6 / 1E6; public static double distance(double lat1, double lon1, double lat2, double lon2) {
double a2 = DEG2RAD * longitudeE6 / 1E6;
double b1 = DEG2RAD * other.latitudeE6 / 1E6; double a1 = DEG2RAD * lat1;
double b2 = DEG2RAD * other.longitudeE6 / 1E6; double a2 = DEG2RAD * lon1;
double b1 = DEG2RAD * lat2;
double b2 = DEG2RAD * lon2;
double cosa1 = Math.cos(a1); double cosa1 = Math.cos(a1);
double cosb1 = Math.cos(b1); double cosb1 = Math.cos(b1);
@ -187,6 +194,6 @@ public class GeoPoint implements Comparable<GeoPoint> {
double tt = Math.acos(t1 + t2 + t3); double tt = Math.acos(t1 + t2 + t3);
return (int) (RADIUS_EARTH_METERS * tt); return (RADIUS_EARTH_METERS * tt);
} }
} }

View File

@ -126,11 +126,11 @@ public class PathLayer extends Layer {
public void addGreatCircle(GeoPoint startPoint, GeoPoint endPoint) { public void addGreatCircle(GeoPoint startPoint, GeoPoint endPoint) {
synchronized (mPoints) { synchronized (mPoints) {
// get the great circle path length in meters /* get the great circle path length in meters */
int length = startPoint.distanceTo(endPoint); double length = startPoint.distanceTo(endPoint);
// add one point for every 100kms of the great circle path /* add one point for every 100kms of the great circle path */
int numberOfPoints = length / 100000; int numberOfPoints = (int) (length / 100000);
addGreatCircle(startPoint, endPoint, numberOfPoints); addGreatCircle(startPoint, endPoint, numberOfPoints);
} }