使用 GMap.NET 将地理坐标转换为静态 google 地图图像中的像素

Translate geo coordinates to pixels in static google maps image using GMap.NET

我正在尝试根据地理位置创建静态地图 ???,??? 并获取该位置附近的 POI(感兴趣的地方),它们的地理位置是必需的,所以我想我需要为它们做一个单独的查询,而不仅仅是在静态搜索中为它们设置标记。

        //transform static map center to pixels
        var center = projection.mercator.FromLatLngToPixel(
            worldPosition.latitude,
            worldPosition.longitude,
            zoom
        );
        //iterate places
        foreach (Place place in map.places)
        {
            //transform place location to pixels
            var point = projection.mercator.FromLatLngToPixel(
                place.location.latitude,
                place.location.longitude,
                zoom
            );
            //calculate pixel position relative to the image center
            var pointRelativeToImage = center - point;

        }

但是,结果有点不正确,所以我的问题是我如何正确地做到这一点? 这是计算像素位置的方法:(GMaps.Net.Projections.MercatorProjection)

  public override GPoint FromLatLngToPixel(double lat, double lng, int zoom)
  {
     GPoint ret = GPoint.Empty;

     lat = Clip(lat, MinLatitude, MaxLatitude);
     lng = Clip(lng, MinLongitude, MaxLongitude);

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

     GSize s = GetTileMatrixSizePixel(zoom);
     long mapSizeX = s.Width;
     long mapSizeY = s.Height;

     ret.X = (long)Clip(x * mapSizeX + 0.5, 0, mapSizeX - 1);
     ret.Y = (long)Clip(y * mapSizeY + 0.5, 0, mapSizeY - 1);

     return ret;
  }

问题解决了。 造成稍微不正确的结果是因为我计算的是相对于中心像素的像素,而我本应相对于左上角的像素计算它们。 google 地图中的 (0,0) 像素。

解决方案:

        //translate place to pixels
        var needlePoint = m_mercator.FromLatLngToPixel(needle.latitude,
            needle.longitude,
            zoom);

        //translate map center to pixel
        var haystackPoint = m_mercator.FromLatLngToPixel(haystackCenter.latitude,
            haystackCenter.longitude,
            zoom);

        //calculate the top left pixel
        var haystackCorner = new Point(haystackPoint.x-(imageSize.x/2),
            haystackPoint.y-(imageSize.y/2));

        //translate the pixel to local space relative to the map
        var point = new Point(needlePoint.x - haystackCorner.x,
            needlePoint.y - haystackCorner.y);

这是假设图像上的 0,0 像素位于左上角,显然我使用的图像 API 在左下角有 0,0 像素。