繁体   English   中英

使用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;
  }

解决了问题。 由于我正在计算相对于中心像素的像素,而我应该相对于左上角像素计算像素,因此导致了略有不正确的结果。 (0,0)像素在Google地图中。

解:

        //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像素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM