From b7affae8abfff16a02c8e78fe40c50a1450e7926 Mon Sep 17 00:00:00 2001 From: Tsanie Lily Date: Fri, 18 Mar 2022 18:44:30 +0800 Subject: [PATCH] separate location extension --- Billing.Shared/Billing.Shared.projitems | 1 + Billing.Shared/Helper.cs | 33 --------------- Billing.Shared/LocationExtension.cs | 54 ++++++++++++++++++++++++ Billing.Shared/Views/ViewLocationPage.cs | 2 +- 4 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 Billing.Shared/LocationExtension.cs diff --git a/Billing.Shared/Billing.Shared.projitems b/Billing.Shared/Billing.Shared.projitems index 3d02ec0..4490d62 100644 --- a/Billing.Shared/Billing.Shared.projitems +++ b/Billing.Shared/Billing.Shared.projitems @@ -22,6 +22,7 @@ + MainShell.xaml Code diff --git a/Billing.Shared/Helper.cs b/Billing.Shared/Helper.cs index 897162c..69223a3 100644 --- a/Billing.Shared/Helper.cs +++ b/Billing.Shared/Helper.cs @@ -147,39 +147,6 @@ namespace Billing } return status; } - - private static double TransformLatitude(double x, double y) - { - var 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 * Math.PI) + 20.0 * Math.Sin(2.0 * x * Math.PI)) * 2.0 / 3.0; - ret += (20.0 * Math.Sin(y * Math.PI) + 40.0 * Math.Sin(y / 3.0 * Math.PI)) * 2.0 / 3.0; - ret += (160.0 * Math.Sin(y / 12.0 * Math.PI) + 320 * Math.Sin(y * Math.PI / 30.0)) * 2.0 / 3.0; - return ret; - } - - private static double TransformLongitude(double x, double y) - { - var 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 * Math.PI) + 20.0 * Math.Sin(2.0 * x * Math.PI)) * 2.0 / 3.0; - ret += (20.0 * Math.Sin(x * Math.PI) + 40.0 * Math.Sin(x / 3.0 * Math.PI)) * 2.0 / 3.0; - ret += (150.0 * Math.Sin(x / 12.0 * Math.PI) + 300.0 * Math.Sin(x / 30.0 * Math.PI)) * 2.0 / 3.0; - return ret; - } - - public static (double longitude, double latitude) Wgs84ToGcj02(double longitude, double latitude) - { - var a = 6378245.0; - var ee = 0.00669342162296594323; - var offsetLatitude = TransformLatitude(longitude - 105.0, latitude - 35.0); - var offsetLongitude = TransformLongitude(longitude - 105.0, latitude - 35.0); - var radiusLatitude = latitude / 180.0 * Math.PI; - var magic = Math.Sin(radiusLatitude); - magic = 1 - ee * magic * magic; - var sqrtMagic = Math.Sqrt(magic); - offsetLatitude = offsetLatitude * 180.0 / (a * (1 - ee) / (magic * sqrtMagic) * Math.PI); - offsetLongitude = offsetLongitude * 180.0 / (a / sqrtMagic * Math.Cos(radiusLatitude) * Math.PI); - return (longitude + offsetLongitude, latitude + offsetLatitude); - } } public class AsyncLazy diff --git a/Billing.Shared/LocationExtension.cs b/Billing.Shared/LocationExtension.cs new file mode 100644 index 0000000..01e2a6a --- /dev/null +++ b/Billing.Shared/LocationExtension.cs @@ -0,0 +1,54 @@ +using System; + +namespace Billing +{ + public static class LocationExtension + { + private const double PI = 3.1415926535897931; + + public static (double longitude, double latitude) Wgs84ToGcj02(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 sqrtMagic = Math.Sqrt(magic); + 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); + } + + private static bool IsOutOfChina(double lon, double lat) + { + return lon < 72.004 || lon > 137.8347 + || lat < 0.8293 || lat > 55.8171; + } + + private static double TransformLatitude(double x, double y) + { + var ret = -100d + 2d * x + 3d * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x)); + ret += (20d * Math.Sin(6d * x * PI) + 20d * Math.Sin(2d * x * PI)) * 2d / 3d; + ret += (20d * Math.Sin(y * PI) + 40d * Math.Sin(y / 3d * PI)) * 2d / 3d; + ret += (160d * Math.Sin(y / 12d * PI) + 320d * Math.Sin(y * PI / 30d)) * 2d / 3d; + return ret; + } + + private static double TransformLongitude(double x, double y) + { + var ret = 300d + x + 2d * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x)); + ret += (20d * Math.Sin(6d * x * PI) + 20d * Math.Sin(2d * x * PI)) * 2d / 3d; + ret += (20d * Math.Sin(x * PI) + 40d * Math.Sin(x / 3d * PI)) * 2d / 3d; + ret += (150d * Math.Sin(x / 12d * PI) + 300d * Math.Sin(x / 30d * PI)) * 2d / 3d; + return ret; + } + + } +} diff --git a/Billing.Shared/Views/ViewLocationPage.cs b/Billing.Shared/Views/ViewLocationPage.cs index f1ef086..2fb6c55 100644 --- a/Billing.Shared/Views/ViewLocationPage.cs +++ b/Billing.Shared/Views/ViewLocationPage.cs @@ -14,7 +14,7 @@ namespace Billing.Views if (bill.Latitude != null && bill.Longitude != null) { - var (longitude, latitude) = Helper.Wgs84ToGcj02(bill.Longitude.Value, bill.Latitude.Value); + var (longitude, latitude) = (bill.Longitude.Value, bill.Latitude.Value).Wgs84ToGcj02(); var position = new Position(latitude, longitude); var mapSpan = new MapSpan(position, 0.01, 0.01); Content = new Map(mapSpan)