Uber app has a polyline that is curved, and even includes a shadow. The shadow may be just a black with transparent polyline connecting two points. That is easy. But the second polyline with curve, how to accomplish this? Is this a Bezier curve, or a built in function like setGeodesic(true)?
I have looked through the google maps examples and I see a section about circle polylines. Can this be adapted to create semi circles? Code snippet from demo.
PolylineOptions options = new PolylineOptions();
int radius = 5; //What is that?
int numPoints = 100;
double phase = 2 * Math.PI / numPoints;
for (int i = 0; i <= numPoints; i++) {
options.add(new LatLng(SYDNEY.latitude + radius * Math.sin(i * phase),
SYDNEY.longitude + radius * Math.cos(i * phase)));
}
int color = Color.RED;
mMap.addPolyline(options
.color(color)
.width(2));
I was able to achieve this with the following bezier curve calculation
double cLat = ((start.latitude + end.latitude) / 2);
double cLon = ((start.longitude + end.longitude) / 2);
//add skew and arcHeight to move the midPoint
if(Math.abs(start.longitude - end.longitude) < 0.0001){
cLon -= 0.0195;
} else {
cLat += 0.0195;
}
double tDelta = 1.0/50;
for (double t = 0; t <= 1.0; t+=tDelta) {
double oneMinusT = (1.0-t);
double t2 = Math.pow(t, 2);
double lon = oneMinusT * oneMinusT * start.longitude
+ 2 * oneMinusT * t * cLon
+ t2 * end.longitude;
double lat = oneMinusT * oneMinusT * start.latitude
+ 2 * oneMinusT * t * cLat
+ t2 * end.latitude;
alLatLng.add(new LatLng(lat, lon));
}
// draw polyline
PolylineOptions line = new PolylineOptions();
line.width(POLYGON_STROKE_WIDTH_PX);
line.color(Color.RED);
line.addAll(alLatLng);
map.addPolyline(line);
private fun plotPolyline(
startLat: Double?,
startLon: Double?,
markerLat: Double?,
markerLon: Double?
) {
if (startLat == null || startLon == null || markerLat == null || markerLon == null) {
return
}
var startPoint = LatLng(startLat, startLon)
var endPoint = LatLng(markerLat, markerLon)
val distance = SphericalUtil.computeDistanceBetween(startPoint, endPoint)
val midPoint = SphericalUtil.interpolate(startPoint, endPoint, 0.5)
val midToStartLocHeading = SphericalUtil.computeHeading(midPoint, startPoint)
val controlPointAngle = 360.0 - (90.0 - midToStartLocHeading)
val controlPoint = SphericalUtil.computeOffset(midPoint, distance / 2.0, controlPointAngle)
var t = 0.0
val polylineOptions = PolylineOptions()
while (t <= 1.00) {
val oneMinusT = 1.0 - t
val lon: Double =
oneMinusT * oneMinusT * startLon + 2 * oneMinusT * t * controlPoint.longitude + t * t * markerLon
val lat: Double =
oneMinusT * oneMinusT * startLat + 2 * oneMinusT * t * controlPoint.latitude + t * t * markerLat
polylineOptions.add(LatLng(lat, lon))
t += 0.05
}
polylineOptions.add(endPoint)
// Draw polyline
polyline?.remove()
var pattern = listOf<PatternItem>(Gap(10.0f), Dash(10.0f))
polyline = googleMap?.addPolyline(
polylineOptions.width(10f).pattern(pattern)
.geodesic(false)
)
}
Related
I'm trying to generate some points at random distances away from a fixed point using GPS.
How can I add distance in meters to a GPS coordinate?
I've looked at UTM to GPS conversion but is there a simpler method to achieve this?
I'm working on Android platform just in case.
Cheers,
fgs
P0(lat0,lon0) : initial position (unit : degrees)
dx,dy : random offsets from your initial position in meters
You can use an approximation to compute the position of the randomized position:
lat = lat0 + (180/pi)*(dy/6378137)
lon = lon0 + (180/pi)*(dx/6378137)/cos(lat0)
This is quite precise as long as the random distance offset is below 10-100 km
Edit: of course in Java Math.cos() expects radians so do use Math.cos(Math.PI/180.0*lat0) if lat0 is in degrees as assumed above.
To take a square I'm using this:
private double[] getBoundingBox(final double pLatitude, final double pLongitude, final int pDistanceInMeters) {
final double[] boundingBox = new double[4];
final double latRadian = Math.toRadians(pLatitude);
final double degLatKm = 110.574235;
final double degLongKm = 110.572833 * Math.cos(latRadian);
final double deltaLat = pDistanceInMeters / 1000.0 / degLatKm;
final double deltaLong = pDistanceInMeters / 1000.0 / degLongKm;
final double minLat = pLatitude - deltaLat;
final double minLong = pLongitude - deltaLong;
final double maxLat = pLatitude + deltaLat;
final double maxLong = pLongitude + deltaLong;
boundingBox[0] = minLat;
boundingBox[1] = minLong;
boundingBox[2] = maxLat;
boundingBox[3] = maxLong;
return boundingBox;
}
This returns an array with 4 coordinates, with them you can make a square with your original point in center.
A detailed outline is given at http://www.movable-type.co.uk/scripts/latlong.html.
If you, somewhere, need to interconvert longitude/latitude to UTM coordinates (the ones used in GPS) you may want to have a look at http://www.uwgb.edu/dutchs/UsefulData/UTMFormulas.htm
If you want to go east or north or west or south you can use this:
#SuppressLint("DefaultLocale")
public static double go_mock_loc(double xx_lat,double xx_long,double xx_dinstance,String Direction)
{
// double xx_lat= 45.815005;
// double xx_long= 15.978501;
// int xx_dinstance=500;
int equator_circumference=6371000;
int polar_circumference=6356800;
double m_per_deg_long = 360 / polar_circumference;
double rad_lat=(xx_lat* (Math.PI) / 180);
double m_per_deg_lat = 360 / ( Math.cos(rad_lat) * equator_circumference);
double deg_diff_long = xx_dinstance * m_per_deg_long;
double deg_diff_lat = xx_dinstance * m_per_deg_lat;
double xx_north_lat = xx_lat + deg_diff_long;
//double xx_north_long= xx_long;
double xx_south_lat = xx_lat - deg_diff_long;
//double xx_south_long= xx_long;
//double xx_east_lat = xx_lat;
double xx_east_long= xx_long + deg_diff_lat;
//double xx_west_lat = xx_lat;
double xx_west_long= xx_long - deg_diff_lat;
if (Direction.toUpperCase().contains("NORTH")) {
return xx_north_lat;
} else if (Direction.toUpperCase().contains("SOUTH"))
{
return xx_south_lat;
} else if (Direction.toUpperCase().contains("EAST"))
{
return xx_east_long;
} else if (Direction.toUpperCase().contains("WEST"))
{
return xx_west_long;
}
else
return 0;
}
I found that solution of #Bogdan Khrystov is very well.
So here is C# version of his solution.
public enum GeoDirection
{
NORTH = 1, SOUTH = 2, EAST = 3, WEST = 4
}
public static Tuple<double, double> AddDistanceInMeters(double latitude, double longitude, int distanceInMeters, GeoDirection direction)
{
var equatorCircumference = 6371000;
var polarCircumference = 6356800;
var mPerDegLong = 360 / (double)polarCircumference;
var radLat = latitude * Math.PI / 180;
var mPerDegLat = 360 / (Math.Cos(radLat) * equatorCircumference);
var degDiffLong = distanceInMeters * mPerDegLong;
var degDiffLat = distanceInMeters * mPerDegLat;
var xxNorthLat = latitude + degDiffLong;
var xxSouthLat = latitude - degDiffLong;
var xxEastLong = longitude + degDiffLat;
var xxWestLong = longitude - degDiffLat;
switch (direction)
{
case GeoDirection.NORTH:
return new Tuple<double, double>(xxNorthLat, longitude);
case GeoDirection.SOUTH:
return new Tuple<double, double>(xxSouthLat, longitude);
case GeoDirection.EAST:
return new Tuple<double, double>(latitude, xxEastLong);
case GeoDirection.WEST:
return new Tuple<double, double>(latitude, xxWestLong);
default:
return null;
}
}
rewrite #Ersin Gülbahar answer in Kotlin:
object LocationUtil {
enum class Direction {
NORTH, SOUTH, EAST, WEST
}
fun addDistanceInMeters(
latitude: Double,
longitude: Double,
distanceInMeters: Int,
direction: Direction
): Pair<Double, Double> {
val equatorCircumference = 6371000
val polarCircumference = 6356800
val mPerDegLong = (360 / polarCircumference.toDouble())
val radLat = latitude * Math.PI / 180
val mPerDegLat = 360 / (Math.cos(radLat) * equatorCircumference)
val degDiffLong = distanceInMeters * mPerDegLong
val degDiffLat = distanceInMeters * mPerDegLat
val xxNorthLat = latitude + degDiffLong
val xxSouthLat = latitude - degDiffLong
val xxEastLong = longitude + degDiffLat
val xxWestLong = longitude - degDiffLat
return when (direction) {
Direction.NORTH -> Pair(xxNorthLat, longitude)
Direction.SOUTH -> Pair(xxSouthLat, longitude)
Direction.EAST -> Pair(latitude, xxEastLong)
Direction.WEST -> Pair(latitude, xxWestLong)
}
}
}
This code splits the line between two coordinates in n segments. Replace the delta calculation by your fixed distance
#Override
public void split(Coordinates p1, Coordinates p2, int segments) {
double φ1 = Math.toRadians(p1.getLat());
double λ1 = Math.toRadians(p1.getLon());
double φ2 = Math.toRadians(p2.getLat());
double λ2 = Math.toRadians(p2.getLon());
double xDelta = (φ2 - φ1) / segments;
double yDelta = (λ2 - λ1) / segments;
for (int i = 0; i < segments; i++){
double x = φ1 + i * xDelta;
double y = λ1 + i * yDelta;
double xc = Math.toDegrees(x);
double yc = Math.toDegrees(y);
System.out.println(xc+","+yc);
}
}
Combining answers from #Ersin Gülbahar and #Stéphane above, I came up with this solution in Flutter/Dart:
import 'dart:math' as math;
enum Direction { north, south, east, west }
double moveCoordinate(
double latitude, double longitude, double distanceToMoveInMeters, Direction directionToMove) {
const earthEquatorRadius = 6378137;
final latitudeOffset = (180 / math.pi) * (distanceToMoveInMeters / earthEquatorRadius);
final longitudeOffset = (180 / math.pi) *
(distanceToMoveInMeters / earthEquatorRadius) /
math.cos(math.pi / 180 * latitude);
switch (directionToMove) {
case Direction.north:
return latitude + latitudeOffset;
case Direction.south:
return latitude - latitudeOffset;
case Direction.east:
return longitude + longitudeOffset;
case Direction.west:
return longitude - longitudeOffset;
}
return 0;
}
This works, tested. The code is C# but you can easily change it to another language
private PointLatLng NewPositionBasedOnDistanceAngle(PointLatLng org, double distance, double bearing)
{
double rad = bearing * Math.PI / 180; //to radians
double lat1 = org.Lat * Math.PI / 180; //to radians
double lng1 = org.Lng * Math.PI / 180; //to radians
double lat = Math.Asin(Math.Sin(lat1) * Math.Cos(distance / 6378137) + Math.Cos(lat1) * Math.Sin(distance / 6378137) * Math.Cos(rad));
double lng = lng1 + Math.Atan2(Math.Sin(rad) * Math.Sin(distance / 6378137) * Math.Cos(lat1), Math.Cos(distance / 6378137) - Math.Sin(lat1) * Math.Sin(lat));
return new PointLatLng(lat * 180 / Math.PI, lng * 180 / Math.PI); // to degrees
}
I've been trying to come up with an algorithm to draw an arrow in a custom View, using Path, but I haven't figured out how to get the coordinates of the arrowhead tips. The line startpoint and endpoint coordinates are arbitrary, the angle of the arrowhead relative to the line and the length of the arrowhead are fixed.
I think I have to use trigonometry somehow, but I'm not sure how.
My friend came up with a math equation, which I have translated into java code here:
public static void calculateArrowHead(Point start, Point end, double angleInDeg, double tipLength){
double x1 = end.getX();
double x2 = start.getX();
double y1 = end.getY();
double y2 = start.getY();
double alpha = Math.toRadians(angleInDeg);
double l1 = Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2)); // length of the arrow line
double l2 = tipLength;
double a = Math.pow(y2-y1, 2) + Math.pow(x2-x1, 2);
double b = -2 * l1 * l2 * Math.cos(alpha) * (y2 - y1);
double c = Math.pow(l1, 2) * Math.pow(l2, 2) * Math.pow(Math.cos(alpha), 2) - Math.pow(l2, 2) * Math.pow(x2-x1, 2);
double s2a = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
double s2b = (-b - Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
double s1a = (l1 * l2 * Math.cos(alpha) - s2a * (y2 - y1)) / (x2-x1);
double s1b = (l1 * l2 * Math.cos(alpha) - s2b * (y2 - y1)) / (x2-x1);
double x3a = s1a + x1;
double y3a = s2a + y1;
double x3b = s1b + x1;
double y3b = s2b + y1;
System.out.println("(A) x:" + (int)x3a + "; y:" + (int)y3a);
System.out.println("(B) x:" + (int)x3b + "; y:" + (int)y3b);
}
I haven't tested it thoroughly, but for the first few tests, it appears to be correct.
I am using Google Map API to get lines on the map in my application. I am loading the nodes of the lines from a database using following code:
// Add polyline "walks voda"
List<WalkLine> dbwalknodes = dbclass.queryWalksFromDatabase(this); // list of latlng
for (int i = 0; i < dbwalknodes.size() - 1 ; i++) {
WalkLine source = dbwalknodes.get(i);
WalkLine destination = dbwalknodes.get(i+1);
Polyline line = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(source.getLat(), source.getLon()),
new LatLng(destination.getLat(), destination.getLon()))
.width(16)
.color(Color.parseColor("#1b9e77"))
.geodesic(true));
line.setZIndex(1000);
}
Do you have any idea how to create the lines smoother while it bends than on the picture bellow? Is it possible?
https://www.dropbox.com/s/6waic988mj90kdk/2014-10-22%2012.48.04.png?dl=0
You should not create a polyline for each two points, it should be a connected polyline with mulitple points, something like this:
public void drawRoute(List<LatLng> location) {
polylineOptions = new PolylineOptions().width(MAPS_PATH_WIDTH).color(routeColor).addAll(location);
polyLine = map.addPolyline(destinationRoutePolyLineOptions);
polyLine.setPoints(location);
}
This will make it much smoother.
Use the following code based on bSpline algorithm, it worked for me on Android.
public List<LatLng> bspline(List<LatLng> poly) {
if (poly.get(0).latitude != poly.get(poly.size()-1).latitude || poly.get(0).longitude != poly.get(poly.size()-1).longitude){
poly.add(new LatLng(poly.get(0).latitude,poly.get(0).longitude));
}
else{
poly.remove(poly.size()-1);
}
poly.add(0,new LatLng(poly.get(poly.size()-1).latitude,poly.get(poly.size()-1).longitude));
poly.add(new LatLng(poly.get(1).latitude,poly.get(1).longitude));
Double[] lats = new Double[poly.size()];
Double[] lons = new Double[poly.size()];
for (int i=0;i<poly.size();i++){
lats[i] = poly.get(i).latitude;
lons[i] = poly.get(i).longitude;
}
double ax, ay, bx, by, cx, cy, dx, dy, lat, lon;
float t;
int i;
List<LatLng> points = new ArrayList<>();
// For every point
for (i = 2; i < lats.length - 2; i++) {
for (t = 0; t < 1; t += 0.2) {
ax = (-lats[i - 2] + 3 * lats[i - 1] - 3 * lats[i] + lats[i + 1]) / 6;
ay = (-lons[i - 2] + 3 * lons[i - 1] - 3 * lons[i] + lons[i + 1]) / 6;
bx = (lats[i - 2] - 2 * lats[i - 1] + lats[i]) / 2;
by = (lons[i - 2] - 2 * lons[i - 1] + lons[i]) / 2;
cx = (-lats[i - 2] + lats[i]) / 2;
cy = (-lons[i - 2] + lons[i]) / 2;
dx = (lats[i - 2] + 4 * lats[i - 1] + lats[i]) / 6;
dy = (lons[i - 2] + 4 * lons[i - 1] + lons[i]) / 6;
lat = ax * Math.pow(t + 0.1, 3) + bx * Math.pow(t + 0.1, 2) + cx * (t + 0.1) + dx;
lon = ay * Math.pow(t + 0.1, 3) + by * Math.pow(t + 0.1, 2) + cy * (t + 0.1) + dy;
points.add(new LatLng(lat, lon));
}
}
return points;
}
I researched a little in this topic, but there are many opinions that don't exactly give a clear image. My problem is this: I'm developing a GPS-based app for Android, in which I want to know distance between my current location specified by Androids LocationManager, and other location in real time. I tried Haversine formula, a Law of Cosines formula, then I discovered, that Android SDK gives me a simple function Location.distanceTo(Location) - I'm not sure what method does this function runs on.
So, the point is, which one will be good for me to use, in situations when real distance between these locations most of the time won't be larger than aprox. 100-200m? Maybe I should check Vincenty's formulae? Is it really that slow? Can someone please explain me what should I choose?
Don't use distanceTo. Use the distanceBetween method as it sounds like you already have the coordinates and that's all you need with this method: Location.distanceBetween() Javadoc
Looking into the Android source for distanceTo(Location), you can see that the result is based on the "Inverse Formula" of geodesy:
Which is based on using the "Inverse Formula" (section 4)
Furthermore, the two methods distanceTo and distanceBetween use the same underlying method. They just have alternative forms of input/output.
For completeness, the full source of this computation is included below, but I encourage you to check out the Location class in android.location for yourself. (P.S. I did not check the correctness of the Android computation. This would be a good exercise!)
private static void computeDistanceAndBearing(double lat1, double lon1,
double lat2, double lon2, float[] results) {
// Based on http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf
// using the "Inverse Formula" (section 4)
int MAXITERS = 20;
// Convert lat/long to radians
lat1 *= Math.PI / 180.0;
lat2 *= Math.PI / 180.0;
lon1 *= Math.PI / 180.0;
lon2 *= Math.PI / 180.0;
double a = 6378137.0; // WGS84 major axis
double b = 6356752.3142; // WGS84 semi-major axis
double f = (a - b) / a;
double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);
double L = lon2 - lon1;
double A = 0.0;
double U1 = Math.atan((1.0 - f) * Math.tan(lat1));
double U2 = Math.atan((1.0 - f) * Math.tan(lat2));
double cosU1 = Math.cos(U1);
double cosU2 = Math.cos(U2);
double sinU1 = Math.sin(U1);
double sinU2 = Math.sin(U2);
double cosU1cosU2 = cosU1 * cosU2;
double sinU1sinU2 = sinU1 * sinU2;
double sigma = 0.0;
double deltaSigma = 0.0;
double cosSqAlpha = 0.0;
double cos2SM = 0.0;
double cosSigma = 0.0;
double sinSigma = 0.0;
double cosLambda = 0.0;
double sinLambda = 0.0;
double lambda = L; // initial guess
for (int iter = 0; iter < MAXITERS; iter++) {
double lambdaOrig = lambda;
cosLambda = Math.cos(lambda);
sinLambda = Math.sin(lambda);
double t1 = cosU2 * sinLambda;
double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;
double sinSqSigma = t1 * t1 + t2 * t2; // (14)
sinSigma = Math.sqrt(sinSqSigma);
cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda; // (15)
sigma = Math.atan2(sinSigma, cosSigma); // (16)
double sinAlpha = (sinSigma == 0) ? 0.0 :
cosU1cosU2 * sinLambda / sinSigma; // (17)
cosSqAlpha = 1.0 - sinAlpha * sinAlpha;
cos2SM = (cosSqAlpha == 0) ? 0.0 :
cosSigma - 2.0 * sinU1sinU2 / cosSqAlpha; // (18)
double uSquared = cosSqAlpha * aSqMinusBSqOverBSq; // defn
A = 1 + (uSquared / 16384.0) * // (3)
(4096.0 + uSquared *
(-768 + uSquared * (320.0 - 175.0 * uSquared)));
double B = (uSquared / 1024.0) * // (4)
(256.0 + uSquared *
(-128.0 + uSquared * (74.0 - 47.0 * uSquared)));
double C = (f / 16.0) *
cosSqAlpha *
(4.0 + f * (4.0 - 3.0 * cosSqAlpha)); // (10)
double cos2SMSq = cos2SM * cos2SM;
deltaSigma = B * sinSigma * // (6)
(cos2SM + (B / 4.0) *
(cosSigma * (-1.0 + 2.0 * cos2SMSq) -
(B / 6.0) * cos2SM *
(-3.0 + 4.0 * sinSigma * sinSigma) *
(-3.0 + 4.0 * cos2SMSq)));
lambda = L +
(1.0 - C) * f * sinAlpha *
(sigma + C * sinSigma *
(cos2SM + C * cosSigma *
(-1.0 + 2.0 * cos2SM * cos2SM))); // (11)
double delta = (lambda - lambdaOrig) / lambda;
if (Math.abs(delta) < 1.0e-12) {
break;
}
}
float distance = (float) (b * A * (sigma - deltaSigma));
results[0] = distance;
if (results.length > 1) {
float initialBearing = (float) Math.atan2(cosU2 * sinLambda,
cosU1 * sinU2 - sinU1 * cosU2 * cosLambda);
initialBearing *= 180.0 / Math.PI;
results[1] = initialBearing;
if (results.length > 2) {
float finalBearing = (float) Math.atan2(cosU1 * sinLambda,
-sinU1 * cosU2 + cosU1 * sinU2 * cosLambda);
finalBearing *= 180.0 / Math.PI;
results[2] = finalBearing;
}
}
}
I want to get the Distance between two latitude and longitude in meter / km . So i am using the below function . It gives me the different result than the google Map.
Can you help me to solve my problem ? i dont understand What is the Problem ?
Code
float[] results = {0};
android.location.Location.distanceBetween(lat1, lon1, lat2, lon2, results);
Current Latitude = 23.012281666666663
Current Longitude = 72.51798333333333
Destination Latitude = 23.1120487
Destination Latitude = 72.5766759
It gives this Result = 12579.679 in Meter, while in google map it gives this result = 17.9 Km
I do not understand why this two gives the different result.
This diagram explains why you get 12.5 and 17.6 kms
To calculate the driving distance (17.6 km) , you need to use the directions API
You can use this 2 function to find the distance
*NOTE:*If you cant get the distance correctly then try setting the MILLION var to 1 cause in my case i have multiplied the lat-long values with 1E6 so have to divide with it.
public double calcdist() {
int MILLION = 1000000;
int EARTH_RADIUS_KM = 6371;
double lat1 = la1 / MILLION;
double lon1 = lo1 / MILLION;
double lat2 = la2 / MILLION;
double lon2 = lo2 / MILLION;
double lat1Rad = Math.toRadians(lat1);
double lat2Rad = Math.toRadians(lat2);
double deltaLonRad = Math.toRadians(lon2 - lon1);
double dist = Math
.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad)
* Math.cos(lat2Rad) * Math.cos(deltaLonRad))
* EARTH_RADIUS_KM;
return dist;
}
and
private float round(float dist, int i) {
float p1 = (float) Math.pow(10, i);
dist = dist * p1;
float tmp = Math.round(dist);
return (float) tmp / p1;
}
then use them as
float tempdist = (float) calcdist();
dist = round(tempdist, 2);
Hey I found one source code here and People arround says it will return the correct distance travelling by road. I haven't try this but you may try and tell me if it really works.
GPSSample.java
I dont know how you are passing the Lat - Long Values..Please Try this whole sample code once.
and the code written in android.location.Location.java for this is as below..if you wish can directly use this in your app.
private static void computeDistanceAndBearing(double lat1,
double lon1, double lat2, double lon2, float[] results) {
// Based on http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf
// using the "Inverse Formula" (section 4)
int MAXITERS = 20;
// Convert lat/long to radians
lat1 *= Math.PI / 180.0;
lat2 *= Math.PI / 180.0;
lon1 *= Math.PI / 180.0;
lon2 *= Math.PI / 180.0;
double a = 6378137.0; // WGS84 major axis
double b = 6356752.3142; // WGS84 semi-major axis
double f = (a - b) / a;
double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);
double L = lon2 - lon1;
double A = 0.0;
double U1 = Math.atan((1.0 - f) * Math.tan(lat1));
double U2 = Math.atan((1.0 - f) * Math.tan(lat2));
double cosU1 = Math.cos(U1);
double cosU2 = Math.cos(U2);
double sinU1 = Math.sin(U1);
double sinU2 = Math.sin(U2);
double cosU1cosU2 = cosU1 * cosU2;
double sinU1sinU2 = sinU1 * sinU2;
double sigma = 0.0;
double deltaSigma = 0.0;
double cosSqAlpha = 0.0;
double cos2SM = 0.0;
double cosSigma = 0.0;
double sinSigma = 0.0;
double cosLambda = 0.0;
double sinLambda = 0.0;
double lambda = L; // initial guess
for (int iter = 0; iter < MAXITERS; iter++) {
double lambdaOrig = lambda;
cosLambda = Math.cos(lambda);
sinLambda = Math.sin(lambda);
double t1 = cosU2 * sinLambda;
double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;
double sinSqSigma = t1 * t1 + t2 * t2; // (14)
sinSigma = Math.sqrt(sinSqSigma);
cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda; // (15)
sigma = Math.atan2(sinSigma, cosSigma); // (16)
double sinAlpha = (sinSigma == 0) ? 0.0 : cosU1cosU2
* sinLambda / sinSigma; // (17)
cosSqAlpha = 1.0 - sinAlpha * sinAlpha;
cos2SM = (cosSqAlpha == 0) ? 0.0 : cosSigma - 2.0
* sinU1sinU2 / cosSqAlpha; // (18)
double uSquared = cosSqAlpha * aSqMinusBSqOverBSq; // defn
A = 1
+ (uSquared / 16384.0)
* // (3)
(4096.0 + uSquared
* (-768 + uSquared
* (320.0 - 175.0 * uSquared)));
double B = (uSquared / 1024.0) * // (4)
(256.0 + uSquared
* (-128.0 + uSquared
* (74.0 - 47.0 * uSquared)));
double C = (f / 16.0) * cosSqAlpha
* (4.0 + f * (4.0 - 3.0 * cosSqAlpha)); // (10)
double cos2SMSq = cos2SM * cos2SM;
deltaSigma = B
* sinSigma
* // (6)
(cos2SM + (B / 4.0)
* (cosSigma * (-1.0 + 2.0 * cos2SMSq) - (B / 6.0)
* cos2SM
* (-3.0 + 4.0 * sinSigma * sinSigma)
* (-3.0 + 4.0 * cos2SMSq)));
lambda = L
+ (1.0 - C)
* f
* sinAlpha
* (sigma + C
* sinSigma
* (cos2SM + C * cosSigma
* (-1.0 + 2.0 * cos2SM * cos2SM))); // (11)
double delta = (lambda - lambdaOrig) / lambda;
if (Math.abs(delta) < 1.0e-12) {
break;
}
}
float distance = (float) (b * A * (sigma - deltaSigma));
results[0] = distance;
if (results.length > 1) {
float initialBearing = (float) Math.atan2(
cosU2 * sinLambda, cosU1 * sinU2 - sinU1 * cosU2
* cosLambda);
initialBearing *= 180.0 / Math.PI;
results[1] = initialBearing;
if (results.length > 2) {
float finalBearing = (float) Math.atan2(cosU1
* sinLambda, -sinU1 * cosU2 + cosU1 * sinU2
* cosLambda);
finalBearing *= 180.0 / Math.PI;
results[2] = finalBearing;
}
}
}