c# - 确定缩放级别以覆盖有关纬度/经度的所有标记

标签 c# xamarin.forms

我知道,我所要求的内容存在于 Google Map 中,但我正在使用 Xamarin.Forms.Map,所以..我必须自己制作。

但是,我知道如何获得我的点的中心,即POI(兴趣点),但我不知道如何确定相机的变焦..

我在网上搜索了from this post ,我被重定向到 Haversine的算法

但是,我尝试了给出的代码,但它不起作用..我知道如何找到 POI,即 2 个最远的点,但我无法确定缩放..

请问有什么想法吗? :/

注意:如果您想了解我尝试过的内容,可以使用代码

    #region Camera focus method
    private static void OnCustomPinsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        CustomMap customMap = ((CustomMap)bindable);

        if (customMap.CameraFocusParameter == CameraFocusReference.OnPins)
        {
            List<Position> PositionPins = new List<Position>();
            bool onlyOnePointPresent;

            foreach (CustomPin pin in (newValue as List<CustomPin>))
            {
                PositionPins.Add(pin.Position);
            }
            Position CentralPosition = GetCentralPosition(PositionPins);
            if (PositionPins.Count > 1)
            {
                Position[] FarestPoints = GetTwoFarestPointsOfCenterPointReference(PositionPins, CentralPosition);
                customMap.CameraFocus = GetPositionAndZoomLevelForCameraAboutPositions(FarestPoints);
                onlyOnePointPresent = false;
            }
            else
            {
                customMap.CameraFocus = new CameraFocusData() { Position = CentralPosition };
                onlyOnePointPresent = true;
            }
            customMap.MoveToRegion(MapSpan.FromCenterAndRadius(customMap.CameraFocus.Position,
            (!onlyOnePointPresent) ? (customMap.CameraFocus.Distance) : (new Distance(5))));
        }
    }
    public static Position GetCentralPosition(List<Position> positions)
    {
        if (positions.Count == 1)
        {
            foreach (Position pos in positions)
            {
                return (pos);
            }
        }

        double lat = 0;
        double lng = 0;

        foreach (var pos in positions)
        {
            lat += pos.Latitude;
            lng += pos.Longitude;
        }

        var total = positions.Count;

        lat = lat / total;
        lng = lng / total;

        return new Position(lat, lng);
    }
    public class DataCalc
    {
        public Position Pos { get; set; }
        public double Distance { get; set; }
    }
    public static Position[] GetTwoFarestPointsOfCenterPointReference(List<Position> farestPosition, Position centerPosition)
    {
        Position[] FarestPos = new Position[2];
        List<DataCalc> dataCalc = new List<DataCalc>();

        Debug.WriteLine("So the center is on [{0}]/[{1}]", centerPosition.Latitude, centerPosition.Longitude);

        foreach (Position pos in farestPosition)
        {
            dataCalc.Add(new DataCalc()
            {
                Pos = pos,
                Distance = Math.Sqrt(Math.Pow(pos.Latitude - centerPosition.Latitude, 2) + Math.Pow(pos.Longitude - centerPosition.Longitude, 2))
            });
        }

        DataCalc First = new DataCalc() { Distance = 0 };
        foreach (DataCalc dc in dataCalc)
        {
            if (dc.Distance > First.Distance)
            {
                First = dc;
            }
        }
        Debug.WriteLine("The farest one is on [{0}]/[{1}]", First.Pos.Latitude, First.Pos.Longitude);

        DataCalc Second = new DataCalc() { Distance = 0 };
        foreach (DataCalc dc in dataCalc)
        {
            if (dc.Distance > Second.Distance
                && (dc.Pos.Latitude != First.Pos.Latitude && dc.Pos.Longitude != First.Pos.Longitude))
            {
                Second = dc;
            }
        }
        Debug.WriteLine("the second is on [{0}]/[{1}]", Second.Pos.Latitude, Second.Pos.Longitude);

        FarestPos[0] = First.Pos;
        FarestPos[1] = Second.Pos;

        return (FarestPos);
    }
    public class CameraFocusData
    {
        public Position Position { get; set; }
        public Distance Distance { get; set; }
    }

    //HAVERSINE
    public static CameraFocusData GetPositionAndZoomLevelForCameraAboutPositions(Position[] FarestPoints)
    {
        double earthRadius = 6371000; //metros

        Position pos1 = FarestPoints[0];
        Position pos2 = FarestPoints[1];

        double latitud1Radianes = pos1.Latitude * (Math.PI / 180.0);
        double latitud2Radianes = pos2.Latitude * (Math.PI / 180.0);
        double longitud1Radianes = pos2.Longitude * (Math.PI / 180.0);
        double longitud2Radianes = pos2.Longitude * (Math.PI / 180.0);

        double deltaLatitud = (pos2.Latitude - pos1.Latitude) * (Math.PI / 180.0);
        double deltaLongitud = (pos2.Longitude - pos1.Longitude) * (Math.PI / 180.0);

        double sum1 = Math.Sin(deltaLatitud / 2) * Math.Sin(deltaLatitud / 2);
        double sum2 = Math.Cos(latitud1Radianes) * Math.Cos(latitud2Radianes) * Math.Sin(deltaLongitud / 2) * Math.Sin(deltaLongitud / 2);

        var a = sum1 + sum2;
        var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));

        var distance = earthRadius * c;

        /* lt is deltaLatitud
         * lng is deltaLongitud*/
        var Bx = Math.Cos(latitud2Radianes) * Math.Cos(deltaLongitud);
        var By = Math.Cos(latitud2Radianes) * Math.Sin(deltaLongitud);
        var lt = Math.Atan2(Math.Sin(latitud1Radianes) + Math.Sin(latitud2Radianes),
                            Math.Sqrt((Math.Cos(latitud1Radianes) + Bx) * (Math.Cos(latitud2Radianes) + Bx) + By * By));//Latitud del punto medio
        var lng = longitud1Radianes + Math.Atan2(By, Math.Cos(longitud1Radianes) + Bx);//Longitud del punto medio

        Debug.WriteLine("the final pos of the camera is on [{0}]/[{1}]", lt, lng);

        return (new CameraFocusData() { Position = new Position(lt, lng), Distance = new Distance(distance + 0.2) });
    }
    #endregion

最佳答案

然后我找到了解决方案,有它的代码,它已被写入以放入您的自定义 map 中。

在这里,private static void OnCustomPinsPropertyChanged(BindableObject bindable, object oldValue, object newValue)是我的 List<CustomPins> 调用的方法但您可以使用不同的方法。

public static readonly BindableProperty CustomPinsProperty =
        BindableProperty.Create(nameof(CustomPins), typeof(IList<CustomPin>), typeof(CustomMap), null,
            propertyChanged: OnCustomPinsPropertyChanged);

此外,您可以添加用户的纬度/经度,我没有这样做,因为我的需要没有用户的位置:)。

最后,你可以为缩放添加一个乘数,我的意思是,你可以说,嗯,缩放对我来说太远了,那么好吧,像我一样,将 double distance 相乘。值如 0.70.6 :)

    #region Camera focus definition
    public class CameraFocusData
    {
        public Position Position { get; set; }
        public Distance Distance { get; set; }
    }
    private static void OnCustomPinsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        CustomMap customMap = ((CustomMap)bindable);

        if (customMap.CameraFocusParameter == CameraFocusReference.OnPins)
        {
            List<double> latitudes = new List<double>();
            List<double> longitudes = new List<double>();

            foreach (CustomPin pin in (newValue as List<CustomPin>))
            {
                latitudes.Add(pin.Position.Latitude);
                longitudes.Add(pin.Position.Longitude);
            }

            double lowestLat = latitudes.Min();
            double highestLat = latitudes.Max();
            double lowestLong = longitudes.Min();
            double highestLong = longitudes.Max();
            double finalLat = (lowestLat + highestLat) / 2;
            double finalLong = (lowestLong + highestLong) / 2;

            double distance = DistanceCalculation.GeoCodeCalc.CalcDistance(lowestLat, lowestLong, highestLat, highestLong, DistanceCalculation.GeoCodeCalcMeasurement.Kilometers);

            customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(finalLat, finalLong), Distance.FromKilometers(distance * 0.7)));
        }
    }
    private class DistanceCalculation
    {
        public static class GeoCodeCalc
        {
            public const double EarthRadiusInMiles = 3956.0;
            public const double EarthRadiusInKilometers = 6367.0;

            public static double ToRadian(double val) { return val * (Math.PI / 180); }
            public static double DiffRadian(double val1, double val2) { return ToRadian(val2) - ToRadian(val1); }

            public static double CalcDistance(double lat1, double lng1, double lat2, double lng2)
            {
                return CalcDistance(lat1, lng1, lat2, lng2, GeoCodeCalcMeasurement.Miles);
            }

            public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, GeoCodeCalcMeasurement m)
            {
                double radius = GeoCodeCalc.EarthRadiusInMiles;

                if (m == GeoCodeCalcMeasurement.Kilometers) { radius = GeoCodeCalc.EarthRadiusInKilometers; }
                return radius * 2 * Math.Asin(Math.Min(1, Math.Sqrt((Math.Pow(Math.Sin((DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.Cos(ToRadian(lat1)) * Math.Cos(ToRadian(lat2)) * Math.Pow(Math.Sin((DiffRadian(lng1, lng2)) / 2.0), 2.0)))));
            }
        }

        public enum GeoCodeCalcMeasurement : int
        {
            Miles = 0,
            Kilometers = 1
        }
    }
    #endregion

玩得开心!


2018 年 12 月更新

我开发了一个很久以前在我的存储库上托管的解决方案,与其他 POC https://github.com/Emixam23/XamarinByEmixam23/tree/master/Detailed%20Part/Controls/Map/MapPinsProject

关于c# - 确定缩放级别以覆盖有关纬度/经度的所有标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39536580/

相关文章:

c# - VS 团队测试 : Multiple Test Initialize Methods in Test Class

android - Xamarin.UI 测试异常

android - 错误消息 : The "ConvertResourcesCases" task failed unexpectedly

ios - 无法在 Visual Studio 2015 中添加 iOS 应用程序图标

c# - Response.Redirect 偶尔会忽略 URL 编码

c# - 自定义任务运行程序方法抛出 ArgumentException

oauth-2.0 - Xamarin.Auth Google Presenter 未在 Android Xamarin.Forms 应用程序中关闭

c# - 如何在 xamarin 表单中显示进度条通知

c# - 将 float[] 转换为字符串并返回 float[] - 测试失败,但我不明白为什么

c# - 如何确保线程安全的 ASP.net 页面访问静态对象列表