【问题标题】:Get pixel coordinates of a element on a map in UWP在 UWP 中获取地图上元素的像素坐标
【发布时间】:2016-08-26 06:58:22
【问题描述】:

所以我有一张地图。在它上面我有一些按地理位置定位的 XAML 元素。我需要以像素为单位找到它们的坐标,以便检测它们何时相互重叠(用于分组目的) 我似乎找不到方法。如果我得到MyMap.MapItems,我只会得到绑定到地图的对象集合。 有什么想法吗?

【问题讨论】:

  • 如果您的问题是关于 UWP MapControl,它有一个 GetOffsetFromLocation 方法,可以将地理位置转换为地图上的一个点。无需自己进行任何计算。

标签: c# xaml uwp windows-10 uwp-maps


【解决方案1】:

好问题。我目前有这样的问题。这是一篇准确描述您需要做什么的文章。 https://msdn.microsoft.com/en-us/library/bb259689.aspx?f=255&MSPPError=-2147217396

如果您没有时间阅读代码:

    private const double EarthRadius = 6378137;
    private const double MinLatitude = -85.05112878;
    private const double MaxLatitude = 85.05112878;
    private const double MinLongitude = -180;
    private const double MaxLongitude = 180;

private static double Clip(double n, double minValue, double maxValue)
            {
                return Math.Min(Math.Max(n, minValue), maxValue);
            }

public static uint MapSize(int levelOfDetail)
        {
            return (uint) 256 << levelOfDetail;
        }
public static void LatLongToPixelXY(double latitude, double longitude, int levelOfDetail, out int pixelX, out int pixelY)
        {
            latitude = Clip(latitude, MinLatitude, MaxLatitude);
            longitude = Clip(longitude, MinLongitude, MaxLongitude);

            double x = (longitude + 180) / 360; 
            double sinLatitude = Math.Sin(latitude * Math.PI / 180);
            double y = 0.5 - Math.Log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI);

            uint mapSize = MapSize(levelOfDetail);
            pixelX = (int) Clip(x * mapSize + 0.5, 0, mapSize - 1);
            pixelY = (int) Clip(y * mapSize + 0.5, 0, mapSize - 1);
        }

【讨论】:

  • 哇,这涉及到很多数学。谢谢你。让我试试
  • 这就像一个魅力。对于任何感兴趣的人:它获取相对于地图而不是应用程序框架的像素坐标,这很好。
  • 为了解释,这实现了Mercator Projection的逆变换,它被所有常见的地图控件使用,例如UWP 地图控件。
【解决方案2】:

为什么不使用GetOffsetFromLocation方法的@Clemens 建议?

它会为您完成所有数学运算,即使 MapControl 远离墨卡托投影,它仍然可以工作。

【讨论】:

    猜你喜欢
    • 2010-12-05
    • 2011-08-15
    • 2011-09-26
    • 1970-01-01
    • 2014-02-26
    • 2014-07-16
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多