如何将地理坐标转换为像素? [英] How to convert geographical coordinates to pixels?

查看:104
本文介绍了如何将地理坐标转换为像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发wpf应用程序.我有一张宽度为500高度为500的静态世界地图.我的申请表中有一种表格.在此,用户输入纬度和经度并提交详细信息.我想在静态地图上显示这些经纬度的确切位置.因此,我正在尝试将这些经度和纬度转换为像素.我使用椭圆在静态地图上显示圆.如何在C#中将地理坐标转换为像素?您能提供解决上述问题的任何代码或链接吗?我的问题类似于链接

I am developing wpf application. I have one static world map of 500 width and 500 height. I have one form in my application. In this from user enters the latitude and longitude and submit the details. I want to show the exact location of these latitude and longitude on my static map. So I am trying to convert these latitude and longitude into pixels. I am using ellipse to show the circle on my static map. How should I convert the geographical coordinates into pixels in C# ? Can you please provide me any code or link through which I can solve the above issue ? My question is similar to the link

将long/lat转换为像素x/y在给定的图片上

我发现此链接很有用.

我已经使用了链接

http://code.google.com/p/geographical-dot-net/source/browse/trunk/GeographicalDotNet/GeographicalDotNet/Projection/GoogleMapsAPIProjection.cs

并编写以下代码

GoogleMapsAPIProjection googleApiProjObj = new GoogleMapsAPIProjection(0);
            float x = 18.29F;
            float y = 73.57F;
            System.Drawing.PointF p1 = new System.Drawing.PointF(x,y);
            System.Drawing.PointF p2 =googleApiProjObj.FromCoordinatesToPixel(p1);

            //CircleEllipse.Margin = new Thickness(longitudePixels,latitudePixels, 0, 0);
            CircleEllipse.Margin = new Thickness(p2.X, p2.Y, 0, 0);
            CircleEllipse.Visibility = Visibility.Visible;

18.29和73.57是印度浦那市的经纬度.在上面的代码p2.x中给我141,在p2.y中给我49.所以上面的代码没有向我显示地图上的浦那位置.我的xaml代码如下

18.29 and 73.57 are lat and log of Pune city in India. In the above code p2.x giving me 141 and p2.y giving me 49. So the above code not showing me the Pune location on map. My xaml code is as follows

<ScrollViewer HorizontalScrollBarVisibility="Auto" Grid.Row="4" Width="500" Height="500" Background="Gray">
            <Grid>
                <Image Margin="0,0,0,0" x:Name="MapImage" Source="Images\World-Blank-Map.png" Stretch="Fill" Width="500" Height="500" ></Image>
                <Ellipse Canvas.Top="50" 
      Canvas.Left="50" 
      Fill="Red" 
      Height="5"
      Width="5"
      Visibility="Collapsed"
      StrokeThickness="4"                     

      x:Name="CircleEllipse"
      HorizontalAlignment="Left"
      VerticalAlignment="Top"
      Margin="0,0,0,0" />
            </Grid>
        </ScrollViewer>

推荐答案

您提供了自己的答案,请看下面的代码

You provided your own answer, take a look at the code at the following

http://code.google.com/p/geographical-dot-net/source/browse/trunk/GeographicalDotNet/GeographicalDotNet/Projection/GoogleMapsAPIProjection.cs

public class GoogleMapsAPIProjection
{
    private readonly double PixelTileSize = 256d;
    private readonly double DegreesToRadiansRatio = 180d / Math.PI;
    private readonly double RadiansToDegreesRatio = Math.PI / 180d;
    private readonly PointF PixelGlobeCenter;
    private readonly double XPixelsToDegreesRatio;
    private readonly double YPixelsToRadiansRatio;

    public GoogleMapsAPIProjection(double zoomLevel)
    {
        var pixelGlobeSize = this.PixelTileSize * Math.Pow(2d, zoomLevel);
        this.XPixelsToDegreesRatio = pixelGlobeSize / 360d;
        this.YPixelsToRadiansRatio = pixelGlobeSize / (2d * Math.PI);
        var halfPixelGlobeSize = Convert.ToSingle(pixelGlobeSize / 2d);
        this.PixelGlobeCenter = new PointF(
            halfPixelGlobeSize, halfPixelGlobeSize);
    }

    public PointF FromCoordinatesToPixel(PointF coordinates)
    {
        var x = Math.Round(this.PixelGlobeCenter.X
            + (coordinates.X * this.XPixelsToDegreesRatio));
        var f = Math.Min(
            Math.Max(
                 Math.Sin(coordinates.Y * RadiansToDegreesRatio),
                -0.9999d),
            0.9999d);
        var y = Math.Round(this.PixelGlobeCenter.Y + .5d * 
            Math.Log((1d + f) / (1d - f)) * -this.YPixelsToRadiansRatio);
        return new PointF(Convert.ToSingle(x), Convert.ToSingle(y));
    }

    public PointF FromPixelToCoordinates(PointF pixel)
    {
        var longitude = (pixel.X - this.PixelGlobeCenter.X) /
            this.XPixelsToDegreesRatio;
        var latitude = (2 * Math.Atan(Math.Exp(
            (pixel.Y - this.PixelGlobeCenter.Y) / -this.YPixelsToRadiansRatio))
            - Math.PI / 2) * DegreesToRadiansRatio;
        return new PointF(
            Convert.ToSingle(latitude),
            Convert.ToSingle(longitude));
    }
}

这篇关于如何将地理坐标转换为像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆