Thanks for testing, emexes ! The java source for the Surfacearea2 method is following, if you find anything wrong just point it out:

The code can be easily translated to enable b4a debug.

B4X:

```
/**
* The methods calculates the area on the Earth's spherical enclosed inside a polygon.
* n is the polygon's size.
* GindxLat and GindxLong are the indices of the parameters Lat and Long in the array.
* The returned area is in square Km.
*/
public double SphericalArea2(double [][] Poligon, int n,
int GindxLat, int GindxLong)
{
double meanlat = 0;
double [][] points = new double[n][2] ; // new array because the content must change
for (int i = 0;i<n;i++)
{
points[i][0] = Poligon[i][GindxLat]* deg2rad ; // allocation by the index
points[i][1] = Poligon[i][GindxLong]* deg2rad ;
meanlat += points[i][0];
}
meanlat = meanlat/n ;
return Spherical(points , n, 0, 1, meanlat);
}
private double Spherical(double [][] points , int n, int UindxX, int UindxY, double meanlat)
{
double sum1,sum2,Rs,phis ;
double es = 0.0067394967423 ; // eccentricity
double twoPi = 2 * Math.PI ;
phis = Math.atan(Math.tan(meanlat)/(1+es));
Rs = 6378.137 / Math.pow((1+es*Math.sin(phis)*Math.sin(phis)),0.5); // km
// find angles for point 0
double f1 = Bearing(points[0][0],points[0][1],points[1][0],points[1][1]);
double f2 = Bearing(points[0][0],points[0][1],points[n-1][0],points[n-1][1]);
sum1 = (f1 + twoPi - f2)%twoPi;
sum2 = (f2 + twoPi - f1)%twoPi;
// find angles for point n-1
f1 = Bearing(points[n-1][0],points[n-1][1],points[0][0],points[0][1]);
f2 = Bearing(points[n-1][0],points[n-1][1],points[n-2][0],points[n-2][1]) ;
sum1 += (f1 + twoPi - f2)%twoPi;
sum2 += (f2 + twoPi - f1)%twoPi;
// find angles for the intermediate points
for (int i = 1;i<n-1;i++)
{
f1 = Bearing(points[i][0],points[i][1],points[i+1][0],points[i+1][1]);
f2 = Bearing(points[i][0],points[i][1],points[i-1][0],points[i-1][1]);
sum1 += (f1 + twoPi - f2)%twoPi;
sum2 += (f2 + twoPi - f1)%twoPi;
}
sum1 = Math.min(sum1,sum2) ; // the minimun is the internal angles sum
return Math.abs((Math.abs(sum1)-(n-2)*Math.PI)) * Rs*Rs ;// in sqr km
}
// This method uses arguments in Radians and returns Radians !
private double Bearing(double LatR1,double LongR1,
double LatR2,double LongR2)
{
double range,bearing;
double L = LongR1 - LongR2 ;
double D = (Math.sin(LatR1) * Math.sin(LatR2)) +
(Math.cos(LatR1) * Math.cos(LatR2) *Math.cos(L ));
range = Math.acos(D);
double C = (Math.sin(LatR2) - (Math.sin(LatR1) * D)) /
(Math.cos(LatR1) * Math.sin(range));
if (Math.sin(L) < 0)
{bearing = Math.acos(C) ;}
else
{bearing = (2*Math.PI - Math.acos(C)) ;}
return bearing ;
}
```

Last edited: