在 UWP 中获取地图上元素的像素坐标

Get pixel coordinates of a element on a map in UWP

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

好问题。我目前有这样的问题。这是一篇准确描述您需要做什么的文章。 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);
        }

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

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