public static GeoCoordinate Wgs84ToGcj02(this GeoCoordinate geoCoordinate)
{
if (IsOutOfChina(geoCoordinate))
{
return geoCoordinate;
}
double dLatitude = Wgs84ToGcj02Latitude(geoCoordinate.Longitude - 105d, geoCoordinate.Latitude - 35d);
double dLongitude = Wgs84ToGcj02Longitude(geoCoordinate.Longitude - 105d, geoCoordinate.Latitude - 35d);
double radLat = geoCoordinate.Latitude / 180d * PI;
double magic = Math.Sin(radLat);
magic = 1 - EE * magic * magic;
double sqrtMagic = Math.Sqrt(magic);
dLatitude = (dLatitude * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * PI);
dLongitude = (dLongitude * 180.0) / (A / sqrtMagic * Math.Cos(radLat) * PI);
return new GeoCoordinate(geoCoordinate.Latitude + dLatitude, geoCoordinate.Longitude + dLongitude);
}
private static bool IsOutOfChina(GeoCoordinate geoCoordinate)
{
if (geoCoordinate.Latitude < 0.8293d || geoCoordinate.Latitude > 55.8271d)
{
return true;
}
if (geoCoordinate.Longitude < 72.004d || geoCoordinate.Longitude > 137.8347d)
{
return true;
}
return false;
}
private static double Wgs84ToGcj02Latitude(double x, double y)
{
double latitude = -100d + 2d * x + 3d * y + 0.2d * y * y + 0.1d * x * y + 0.2d * Math.Sqrt(Math.Abs(x));
latitude += (20d * Math.Sin(6d * x * PI) + 20d * Math.Sin(2d * x * PI)) * 2d / 3d;
latitude += (20d * Math.Sin(y * PI) + 40d * Math.Sin(y / 3d * PI)) * 2d / 3d;
latitude += (160d * Math.Sin(y / 12d * PI) + 320d * Math.Sin(y * PI / 30d)) * 2d / 3d;
return latitude;
}
private static double Wgs84ToGcj02Longitude(double x, double y)
{
double longitude = 300d + x + 2d * y + 0.1d * x * x + 0.1d * x * y + 0.1d * Math.Sqrt(Math.Abs(x));
longitude += (20d * Math.Sin(6d * x * PI) + 20d * Math.Sin(2d * x * PI)) * 2d / 3d;
longitude += (20d * Math.Sin(x * PI) + 40d * Math.Sin(x / 3d * PI)) * 2d / 3d;
longitude += (150d * Math.Sin(x / 12d * PI) + 300d * Math.Sin(x / 30d * PI)) * 2d / 3d;
return longitude;
}