花了几个小时把js的google计算地图面积的算法改成了c# 的。
class Program
{
static void Main(string[] args)
{
// a = new qq.maps.LatLng(39.93, 116.44004334);
//b = new qq.maps.LatLng(39.93, 116.35421264);
//c = new qq.maps.LatLng(39.896775, 116.35421264);
//d = new qq.maps.LatLng(39.89, 116.44004334);
//29869510.924133233平方米 List<LatLng> a = new List<LatLng>();
a.Add(new LatLng() { Latitude = 39.93, Longitude = 116.44004334 });
a.Add(new LatLng() { Latitude = 39.93, Longitude = 116.35421264 });
a.Add(new LatLng() { Latitude = 39.896775, Longitude = 116.35421264 });
a.Add(new LatLng() { Latitude = 39.89, Longitude = 116.44004334 }); double area = computeArea(a, ); } static double computeArea(List<LatLng> x, double y)
{
return Math.Abs(computeSignedArea(x, y));
} static double computeSignedArea(List<LatLng> a, double b)
{
double c = ;
if (b > )
{
c = b;
}
double g = a.Count - ;
double e = ;
LatLng d = a[];
for (int i = ; i < g; i++)
{
e += Zm(d, a[i], a[i + ]);
}
return e * c * c;
} static double Zm(LatLng a, LatLng b, LatLng c)
{
return Tm(a, b, c) * Um(a, b, c);
} static double Tm(LatLng a, LatLng b, LatLng c)
{
List<LatLng> d = new List<LatLng>() { a, b, c, a };
List<double> e = new List<double>();
double f = ; for (int i = ; i < d.Count - ; i++)
{
e.Add(Uf(d[i], d[i + ]));
f += e[i];
}
f /= ;
double g = Math.Tan(f / ); for (int j = ; j < d.Count - ; j++)
{
g *= Math.Tan((f - e[j]) / );
} return * Math.Atan(Math.Sqrt(Math.Abs(g)));
}
static double Um(LatLng a, LatLng b, LatLng c)
{
List<LatLng> d = new List<LatLng>() { a, b, c }; double[,] e = new double[, ]; for (int i = ; i < 3; i++)
{
LatLng f = d[i];
double f1 = re(f);
double f2 = se(f);
e[i, ] = Math.Cos(f1) * Math.Cos(f2);
e[i, ] = Math.Cos(f1) * Math.Sin(f2);
e[i, ] = Math.Sin(f1);
}
return < e[, ] * e[, ] * e[, ] + e[, ] * e[, ] * e[, ] + e[, ] * e[, ] * e[, ] - e[, ] * e[, ] * e[, ] - e[, ] * e[, ] * e[, ] - e[, ] * e[, ] * e[, ] ? : -;
} static double Uf(LatLng a, LatLng b)
{
var c = re(a);
var d = re(b);
return * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin((c - d) / ), ) + Math.Cos(c) * Math.Cos(d) * Math.Pow(Math.Sin((se(a) - se(b)) / ), )));
} static double re(LatLng a)
{
return Rd(a.Latitude);
}
static double se(LatLng a)
{
return Rd(a.Longitude);
} static double Rd(double a)
{
return Math.PI / * a;
} } partial struct LatLng
{
private double _Longitude;
public double Longitude
{
get
{
return _Longitude;
}
set
{
_Longitude = value;
}
} private double _Latitude;
public double Latitude
{
get
{
return _Latitude;
}
set
{
_Latitude = value;
}
} }
double computeDistanceBetween(LngLat a, LngLat b, double c)
{
return Uf(a, b) * c;
}
public double ComputeLength(List<LngLat> a, double b = )
{
if (a.Count < )
{
return ;
}
double c = ;
if (b > )
{
c = b;
}
double g = a.Count - ;
double d = ; for (var e = ; e < g; ++e)
{
d += computeDistanceBetween(a[e], a[e + ], c);
}
return d;
}