2012-10-05 7 views
1

Je développe une application wpf. J'ai une carte du monde statique de 500 largeurs et de 500 hauteurs. J'ai un formulaire dans ma demande. Dans cet utilisateur entre la latitude et la longitude et soumettre les détails. Je veux montrer l'emplacement exact de ces latitude et longitude sur ma carte statique. J'essaie donc de convertir ces latitude et longitude en pixels. J'utilise ellipse pour montrer le cercle sur ma carte statique. Comment devrais-je convertir les coordonnées géographiques en pixels en C#? Pouvez-vous s'il vous plaît me fournir tout code ou lien à travers lequel je peux résoudre le problème ci-dessus? Ma question est semblable au lienComment convertir des coordonnées géographiques en pixels?

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

J'ai trouvé ce lien utile.

Edit: J'ai utilisé le lien

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

et écrit le code suivant

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 et 73,57 sont lat et le journal de la ville de Pune en Inde. Dans le code p2.x ci-dessus me donnant 141 et p2.y me donnant 49. Donc le code ci-dessus ne me montrant pas l'emplacement de Pune sur la carte. Mon code XAML est comme suit

<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

Je suis en train d'utiliser le code C# suggéré par @Jader Dias dans le lien –

+1

Quelle projection que votre « statique carte du monde " utilisation? Est-ce une carte statique Google? – Marcelo

+0

Bonjour @Mercelo. Ma carte utilise Mercator Projection –

Répondre

2

Vous avez fourni votre propre réponse, jetez un oeil sur le code à l'adresse suivante

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

Salut @Marcelo. J'ai mis à jour ma question? Pouvez-vous s'il vous plaît me dire où je vais mal –

+0

J'ai fait le copier-coller de la classe GoogleMapsAPIProjection. Devrions-nous spécifier la largeur et la hauteur de la carte n'importe où dans la classe GoogleMapsAPIProjection? Dois-je faire des changements dans la classe GoogleMapsAPIProjection? –

0

Je poste mon api Mercator Projection pour Google Maps. ;-) Vous avez trois classes:

  • Constantes
  • PointCoordinates: latitude et longitude.
  • PointPixel: x, y pour le niveau 0 de Google Maps.

    Vous pouvez convertir PointPixel de/vers 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 }; 

    } 


} 
Questions connexes