location
This commit is contained in:
parent
b7affae8ab
commit
ba289b6087
@ -5,24 +5,58 @@ namespace Billing
|
|||||||
public static class LocationExtension
|
public static class LocationExtension
|
||||||
{
|
{
|
||||||
private const double PI = 3.1415926535897931;
|
private const double PI = 3.1415926535897931;
|
||||||
|
private const double A = 6378245d;
|
||||||
|
private const double EE = 0.00669342162296594323;
|
||||||
|
|
||||||
public static (double longitude, double latitude) Wgs84ToGcj02(this (double lon, double lat) position)
|
public static (double longitude, double latitude) Wgs84ToGcj02(this (double lon, double lat) position)
|
||||||
|
{
|
||||||
|
return Transform(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (double longitude, double latitude) Gcj02ToWgs84(this (double lon, double lat) position)
|
||||||
|
{
|
||||||
|
var (longitude, latitude) = Transform(position);
|
||||||
|
longitude = position.lon * 2d - longitude;
|
||||||
|
latitude = position.lat * 2d - latitude;
|
||||||
|
return (longitude, latitude);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (double longitude, double latitude) Gcj02ToBd09(this (double lon, double lat) position)
|
||||||
|
{
|
||||||
|
double x = position.lon;
|
||||||
|
double y = position.lat;
|
||||||
|
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 longitude = z * Math.Cos(theta) + 0.0065;
|
||||||
|
double latitude = z * Math.Sin(theta) + 0.006;
|
||||||
|
return (longitude, latitude);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (double longitude, double latitude) Bd09ToGcj02(this (double lon, double lat) position)
|
||||||
|
{
|
||||||
|
double x = position.lon - 0.0065;
|
||||||
|
double y = position.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 longitude = z * Math.Cos(theta);
|
||||||
|
double latitude = z * Math.Sin(theta);
|
||||||
|
return (longitude, latitude);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static (double longitude, double latitude) Transform(this (double lon, double lat) position)
|
||||||
{
|
{
|
||||||
if (IsOutOfChina(position.lon, position.lat))
|
if (IsOutOfChina(position.lon, position.lat))
|
||||||
{
|
{
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
var a = 6378245d;
|
|
||||||
var ee = 0.00669342162296594323;
|
|
||||||
|
|
||||||
var offsetLatitude = TransformLatitude(position.lon - 105d, position.lat - 35d);
|
var offsetLatitude = TransformLatitude(position.lon - 105d, position.lat - 35d);
|
||||||
var offsetLongitude = TransformLongitude(position.lon - 105d, position.lat - 35d);
|
var offsetLongitude = TransformLongitude(position.lon - 105d, position.lat - 35d);
|
||||||
var radiusLatitude = position.lat / 180d * PI;
|
var radiusLatitude = position.lat / 180d * PI;
|
||||||
var magic = 1d - ee * Math.Pow(Math.Sin(radiusLatitude), 2);
|
var magic = 1d - EE * Math.Pow(Math.Sin(radiusLatitude), 2);
|
||||||
var sqrtMagic = Math.Sqrt(magic);
|
var sqrtMagic = Math.Sqrt(magic);
|
||||||
offsetLatitude = offsetLatitude * 180d / (a * (1d - ee) / (magic * sqrtMagic) * PI);
|
offsetLatitude = offsetLatitude * 180d / (A * (1d - EE) / (magic * sqrtMagic) * PI);
|
||||||
offsetLongitude = offsetLongitude * 180d / (a / sqrtMagic * Math.Cos(radiusLatitude) * PI);
|
offsetLongitude = offsetLongitude * 180d / (A / sqrtMagic * Math.Cos(radiusLatitude) * PI);
|
||||||
return (position.lon + offsetLongitude, position.lat + offsetLatitude);
|
return (position.lon + offsetLongitude, position.lat + offsetLatitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user