public static void Main()
Console.WriteLine("Hello World");
Console.WriteLine("Lat: {0}, Lon: {1}",p.Lat,p.Lon);
const double pi = 3.14159265358979324;
const double a = 6378245.0;
const double ee = 0.00669342162296594323;
public static LocationPoint GcjToWgs(double lat, double lon)
LocationPoint gps = Transform(lat, lon);
double lontitude = lon * 2 - gps.Lon;
double latitude = lat * 2 - gps.Lat;
return new LocationPoint(latitude, lontitude);
public static LocationPoint GcjToBaidu(double lat, double lon)
double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * pi);
double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * pi);
double bd_lon = z * Math.Cos(theta) + 0.0065;
double bd_lat = z * Math.Sin(theta) + 0.006;
return new LocationPoint(bd_lat, bd_lon);
public static LocationPoint BaiduToGcj(double lat, double lon)
double x = lon - 0.0065, y = lat - 0.006;
double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * pi);
double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * pi);
double gg_lon = z * Math.Cos(theta);
double gg_lat = z * Math.Sin(theta);
return new LocationPoint(gg_lat, gg_lon);
public static LocationPoint BaiduToWgs(double lat, double lon)
LocationPoint gcj02 = BaiduToGcj(lat, lon);
LocationPoint map84 = GcjToWgs(gcj02.Lat,gcj02.Lon);
static bool OutOfChina(double lat, double lon)
if (lon < 72.004 || lon > 137.8347)
if (lat < 0.8293 || lat > 55.8271)
public static LocationPoint Transform(double lat, double lon)
if (OutOfChina(lat, lon))
return new LocationPoint(lat, lon);
double dLat = TransformLat(lon - 105.0, lat - 35.0);
double dLon = TransformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * pi;
double magic = Math.Sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.Sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
return new LocationPoint(mgLat, mgLon);
static double TransformLat(double x, double y)
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x));
ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
static double TransformLon(double x, double y)
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
public class LocationPoint{
public LocationPoint(double lat,double lon){
public double Lat {get;set;}
public double Lon {get;set;}