【问题标题】:Xamarin Forms Map - Center and Distance Update Multiple PinsXamarin Forms Map - 中心和距离更新多个引脚
【发布时间】:2017-02-14 18:41:04
【问题描述】:

我正在加载并向 Xamarin Forms Map 添加图钉。

使用以下方法获取中心纬度/经度:

    public static Position GetCenter(List<Position> geoCoordinates)
    {
        if (geoCoordinates.Count == 1)
        {
            return geoCoordinates.Single();
        }

        double x = 0;
        double y = 0;
        double z = 0;

        foreach (var geoCoordinate in geoCoordinates)
        {
            var latitude = geoCoordinate.Latitude * Math.PI / 180;
            var longitude = geoCoordinate.Longitude * Math.PI / 180;

            x += Math.Cos(latitude) * Math.Cos(longitude);
            y += Math.Cos(latitude) * Math.Sin(longitude);
            z += Math.Sin(latitude);
        }

        var total = geoCoordinates.Count;

        x = x / total;
        y = y / total;
        z = z / total;

        var centralLongitude = Math.Atan2(y, x);
        var centralSquareRoot = Math.Sqrt(x * x + y * y);
        var centralLatitude = Math.Atan2(z, centralSquareRoot);

        var dtoReturn = new Position(centralLatitude * 180 / Math.PI, centralLongitude * 180 / Math.PI);

        return dtoReturn;
    }

这让我成为了中心:

        map.MoveToRegion(MapSpan.FromCenterAndRadius(CENTER, Distance.FromMiles(X));

我的问题是关于Radius 的第二个参数。以英里为单位计算半径的最佳方法是什么?

Distance.FromMiles(X)

【问题讨论】:

  • 你有位置的中心,所以对于半径,你不想找到离中心最远的点,然后离开那个点和中心之间的距离吗?您的潜在数据集有多大?
  • 嘿@deckertron_9000 这正是我现在正在做的事情!但是,当这些点相距几百英里时,它似乎无法正常工作。不知道是否有更精确的方法。

标签: google-maps xamarin.ios xamarin.forms


【解决方案1】:

您可以从中心、纬度度和经度度创建MapSpan,如下所示。

private static MapSpan FromPositions(IEnumerable<Position> positions)
{
    double minLat = double.MaxValue;
    double minLon = double.MaxValue;
    double maxLat = double.MinValue;
    double maxLon = double.MinValue;

    foreach (var p in positions)
    {
        minLat = Math.Min(minLat, p.Latitude);
        minLon = Math.Min(minLon, p.Longitude);
        maxLat = Math.Max(maxLat, p.Latitude);
        maxLon = Math.Max(maxLon, p.Longitude);
    }

    return new MapSpan(
        new Position((minLat + maxLat) / 2d, (minLon + maxLon) / 2d),
        maxLat - minLat, 
        maxLon - minLon);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-29
    • 2019-01-17
    • 2019-04-23
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多