2012-10-05 75 views
1

我正在開發wpf應用程序。我有一個500寬度和500高度的靜態世界地圖。我的應用程序中有一個表單。在這裏從用戶輸入經緯度並提交細節。我想在靜態地圖上顯示這些經度和緯度的確切位置。所以我試圖將這些經度和緯度轉換爲像素。我正在使用橢圓在我的靜態地圖上顯示圓。我應該如何將地理座標轉換爲C#中的像素?你能否給我提供任何可以解決上述問題的代碼或鏈接?我的問題是類似的鏈接如何將地理座標轉換爲像素?

Convert long/lat to pixel x/y on a given picture

我發現這個鏈接有用。

編輯: 我已經使用了鏈接

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代碼如下

<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> 
+0

我試圖使用由@Jader迪亞斯提出的C#代碼中的鏈接 –

+1

什麼投影做你的「靜態的世界地圖「 使用?它是Google靜態地圖嗎? – Marcelo

+0

你好@Mercelo。我的地圖使用墨卡託投影 –

回答

2

你提供你自己的答案,看一看代碼在以下

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)); 
    } 
} 
+0

Hi @Marcelo。我更新了我的問題?你能告訴我我要去哪裏嗎 –

+0

我已經完成了GoogleMapsAPIProjection類的複製和粘貼。我們是否需要在類GoogleMapsAPIProjection中的任何位置指定地圖的寬度和高度?我是否需要在類GoogleMapsAPIProjection中進行任何更改? –

0

我後我的API墨卡託投影谷歌地圖。 ;-)您有三類:

  • 常量
  • PointCoordinates:經度和緯度。
  • PointPixel:x,y爲谷歌地圖的0級。

    可以PointPixel轉換自/至PointCoordinates

{

public static class PointUtils 
{ 
    public const double MercatorGoogleHeight = 256; 
    public const double MercatorGoogleWidth = 256; 
    public const double PixelLongintudeOrigin = MercatorGoogleWidth/2; 
    public const double PixelLatitudeOrigin = MercatorGoogleHeight/2; 
    public const double PixelsPerLongintudeDegre = MercatorGoogleWidth/360; 
    public const double RadsPerLatitudeDegre = MercatorGoogleHeight/(2 * Math.PI); 
} 

/// <summary> 
/// Point Pixel on Mercator Google Zoom 0 
/// </summary> 
public class PointPixel 
{ 
    public double X { get; set; } 
    public double Y { get; set; } 

    public PointCoordinates ToPointCoordinates() 
    { 
     var lng = (X - PointUtils.PixelLongintudeOrigin)/PointUtils.PixelsPerLongintudeDegre; 
     var latRad = (Y - PointUtils.PixelLatitudeOrigin)/-PointUtils.RadsPerLatitudeDegre; 
     var lat = (2*Math.Atan(Math.Exp(latRad)) - Math.PI/2).RadToDeg(); 
     return new PointCoordinates() 
      { 
       Latitude = lat, 
       Longitude = lng 
      }; 
    } 

} 

/// <summary> 
/// Point on Map World 
/// </summary> 
public class PointCoordinates 
{ 

    public double Latitude { get; set; } 
    public double Longitude { get; set; } 

    public PointPixel ToPointPixel() 
    { 
     var x = PointUtils.PixelLongintudeOrigin + PointUtils.PixelsPerLongintudeDegre * Longitude; 
     var siny = Math.Sin(Latitude.DegToRad()); 
     var y = PointUtils.PixelLatitudeOrigin - (Math.Log((1 + siny)/(1 - siny))/2) * PointUtils.RadsPerLatitudeDegre; 
     return new PointPixel() { X = x, Y = y }; 

    } 


} 
相關問題