B4J Question [SOLVED] Coordinates (LAT/LNG) to PIXELs

Magma

Expert
Licensed User
Longtime User
Hi there...

just found two ways to do that... but i need help...

one way is in different language so need to translate it to B4X...

B4X:
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));
    }
}

And other way is more simpler (if supported by developers) but need something to change at library... i think... at jgooglemaps...

The main GMap2 class provides transformation to/from a pixel on the displayed map and a lat/long coordinate:

Gmap2.fromLatLngToContainerPixel(latlng)

as i readed here: https://stackoverflow.com/questions/2651099/convert-long-lat-to-pixel-x-y-on-a-given-picture
 

Magma

Expert
Licensed User
Longtime User
.... YES I DID IT !

@Erel that... helped me a lot :)

Anyone who need that... i have it here:

B4X:
Sub getpixelxy(ll As LatLng) As Double()
    Dim scale As Double
    scale=Power(2,gmap.CameraPosition.Zoom)
    Dim nw As LatLng
    nw.Initialize(gmap.Bounds.NorthEast.Latitude,gmap.Bounds.SouthWest.Longitude)
    Dim worldCoordinateNW() As Double
    worldCoordinateNW=LatLonToXY(nw)
    Dim worldCoordinate() As Double
    worldCoordinate=LatLonToXY(ll)
    Dim sx As Double
    Dim sy As Double
    sx=Floor((worldCoordinate(0) - worldCoordinateNW(0)) * scale)
    sy=Floor((worldCoordinate(1) - worldCoordinateNW(1)) * scale)
    Return Array As Double(sx,sy)
End Sub
 
Upvote 0
Top