From ba289b60871ef888c757e9cf852682441432e4bd Mon Sep 17 00:00:00 2001 From: Tsanie Lily Date: Mon, 21 Mar 2022 11:04:37 +0800 Subject: [PATCH] location --- Billing.Shared/LocationExtension.cs | 46 +++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/Billing.Shared/LocationExtension.cs b/Billing.Shared/LocationExtension.cs index 01e2a6a..0913923 100644 --- a/Billing.Shared/LocationExtension.cs +++ b/Billing.Shared/LocationExtension.cs @@ -5,24 +5,58 @@ namespace Billing public static class LocationExtension { 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) + { + 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)) { return position; } - var a = 6378245d; - var ee = 0.00669342162296594323; - var offsetLatitude = TransformLatitude(position.lon - 105d, position.lat - 35d); var offsetLongitude = TransformLongitude(position.lon - 105d, position.lat - 35d); 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); - offsetLatitude = offsetLatitude * 180d / (a * (1d - ee) / (magic * sqrtMagic) * PI); - offsetLongitude = offsetLongitude * 180d / (a / sqrtMagic * Math.Cos(radiusLatitude) * PI); + offsetLatitude = offsetLatitude * 180d / (A * (1d - EE) / (magic * sqrtMagic) * PI); + offsetLongitude = offsetLongitude * 180d / (A / sqrtMagic * Math.Cos(radiusLatitude) * PI); return (position.lon + offsetLongitude, position.lat + offsetLatitude); }