9

私はGoogleStaticMapsを使用して行うJAVAプロジェクトを持っていますが、何時間も働いた後、何も機能しません。すべてを説明し、誰かが私を助けてくれることを願っています。

静的マップ(480ピクセルx 480ピクセル)を使用しています。マップの中心はlat = 47、lon = 1.5、ズームレベルは5です。

今必要なのは、この静的マップ上のピクセルをクリックしたときにlatとlonを取得できることです。いくつか検索したところ、メルカトル図法を使用する必要があることがわかりました(右?)。また、各ズームレベルで、水平方向と垂直方向の両方の寸法の精度が2倍になることがわかりましたが、ピクセル、ズームレベル、および緯度をリンクするための正しい式が見つかりません。 /lon..。

私の問題は、中心の座標とピクセル、およびズームレベルを知って、ピクセルから緯度/経度を取得することだけです...

前もって感謝します !

4

4 に答える 4

2

メルカトル図法を使用します。

[0, 256)byの空間に射影する場合[0,256]:

LatLng(47,=1.5) is Point(129.06666666666666, 90.04191318303863)

ズーム レベル 5 では、これらはピクセル座標に相当します。

x = 129.06666666666666 * 2^5 = 4130
y = 90.04191318303863 * 2^5 = 2881

したがって、マップの左上は次の場所にあります。

x = 4130 - 480/2 = 4070
y = 2881 - 480/2 = 2641

4070 / 2^5 = 127.1875
2641 / 2^5 = 82.53125

ついに:

Point(127.1875, 82.53125) is LatLng(53.72271667491848, -1.142578125)
于 2012-05-04T20:45:27.733 に答える
1

Google マップは、マップにタイルを使用して、世界を 256^21 ピクセルのタイルのグリッドに効率的に分割します。基本的に、世界は最低ズームで 4 つのタイルで構成されています。ズームを開始すると、16 タイル、64 タイル、256 タイルが表示されます。基本的に四分木です。このような 1 次元構造は 2 次元しか平坦化できないため、メルカントール図法またはWGS 84 への変換も必要です。Google マップには、緯度と経度のペアをピクセルに変換する機能があります。ここにリンクがありますが、タイルは 128x128 のみであると書かれています: http://michal.guerquin.com/googlemaps.html

  1. Google マップ V3 - 特定の境界のズーム レベルを計算する方法
  2. http://www.physicsforums.com/showthread.php?t=455491
于 2012-05-04T05:47:57.357 に答える
1

上記の Chris Broadfoot の回答の数学とMercator Projection の Stack Overflow の他のコードに基づいて、これを得ました

public class MercatorProjection implements Projection {

    private static final double DEFAULT_PROJECTION_WIDTH = 256;
    private static final double DEFAULT_PROJECTION_HEIGHT = 256;

    private double centerLatitude;
    private double centerLongitude;
    private int areaWidthPx;
    private int areaHeightPx;
    // the scale that we would need for the a projection to fit the given area into a world view (1 = global, expect it to be > 1)
    private double areaScale;

    private double projectionWidth;
    private double projectionHeight;
    private double pixelsPerLonDegree;
    private double pixelsPerLonRadian;

    private double projectionCenterPx;
    private double projectionCenterPy;

    public MercatorProjection(
            double centerLatitude,
            double centerLongitude,
            int areaWidthPx,
            int areaHeightPx,
            double areaScale
    ) {
        this.centerLatitude = centerLatitude;
        this.centerLongitude = centerLongitude;
        this.areaWidthPx = areaWidthPx;
        this.areaHeightPx = areaHeightPx;
        this.areaScale = areaScale;

        // TODO stretch the projection to match to deformity at the center lat/lon?
        this.projectionWidth = DEFAULT_PROJECTION_WIDTH;
        this.projectionHeight = DEFAULT_PROJECTION_HEIGHT;
        this.pixelsPerLonDegree = this.projectionWidth / 360;
        this.pixelsPerLonRadian = this.projectionWidth / (2 * Math.PI);

        Point centerPoint = projectLocation(this.centerLatitude, this.centerLongitude);
        this.projectionCenterPx = centerPoint.x * this.areaScale;
        this.projectionCenterPy = centerPoint.y * this.areaScale;
    }

    @Override
    public Location getLocation(int px, int py) {
        double x = this.projectionCenterPx + (px - this.areaWidthPx / 2);
        double y = this.projectionCenterPy + (py - this.areaHeightPx / 2);

        return projectPx(x / this.areaScale, y / this.areaScale);
    }

    @Override
    public Point getPoint(double latitude, double longitude) {
        Point point = projectLocation(latitude, longitude);

        double x = (point.x * this.areaScale - this.projectionCenterPx) + this.areaWidthPx / 2;
        double y = (point.y * this.areaScale - this.projectionCenterPy) + this.areaHeightPx / 2;

        return new Point(x, y);
    }

    // from https://stackoverflow.com/questions/12507274/how-to-get-bounds-of-a-google-static-map

    Location projectPx(double px, double py) {
        final double longitude = (px - this.projectionWidth/2) / this.pixelsPerLonDegree;
        final double latitudeRadians = (py - this.projectionHeight/2) / -this.pixelsPerLonRadian;
        final double latitude = rad2deg(2 * Math.atan(Math.exp(latitudeRadians)) - Math.PI / 2);
        return new Location() {
            @Override
            public double getLatitude() {
                return latitude;
            }

            @Override
            public double getLongitude() {
                return longitude;
            }
        };
    }

    Point projectLocation(double latitude, double longitude) {
        double px = this.projectionWidth / 2 + longitude * this.pixelsPerLonDegree;
        double siny = Math.sin(deg2rad(latitude));
        double py = this.projectionHeight / 2 + 0.5 * Math.log((1 + siny) / (1 - siny) ) * -this.pixelsPerLonRadian;
        Point result = new org.opencv.core.Point(px, py);
        return result;
    }

    private double rad2deg(double rad) {
        return (rad * 180) / Math.PI;
    }

    private double deg2rad(double deg) {
        return (deg * Math.PI) / 180;
    }
}

元の回答の単体テストは次のとおりです

public class MercatorProjectionTest {

    @Test
    public void testExample() {

        // tests against values in https://stackoverflow.com/questions/10442066/getting-lon-lat-from-pixel-coords-in-google-static-map

        double centerLatitude = 47;
        double centerLongitude = 1.5;

        int areaWidth = 480;
        int areaHeight = 480;

        // google (static) maps zoom level
        int zoom = 5;

        MercatorProjection projection = new MercatorProjection(
                centerLatitude,
                centerLongitude,
                areaWidth,
                areaHeight,
                Math.pow(2, zoom)
        );

        Point centerPoint = projection.projectLocation(centerLatitude, centerLongitude);
        Assert.assertEquals(129.06666666666666, centerPoint.x, 0.001);
        Assert.assertEquals(90.04191318303863, centerPoint.y, 0.001);

        Location topLeftByProjection = projection.projectPx(127.1875, 82.53125);
        Assert.assertEquals(53.72271667491848, topLeftByProjection.getLatitude(), 0.001);
        Assert.assertEquals(-1.142578125, topLeftByProjection.getLongitude(), 0.001);

        // NOTE sample has some pretty serious rounding errors
        Location topLeftByPixel = projection.getLocation(0, 0);
        Assert.assertEquals(53.72271667491848, topLeftByPixel.getLatitude(), 0.05);
        // the math for this is wrong in the sample (see comments)
        Assert.assertEquals(-9, topLeftByPixel.getLongitude(), 0.05);

        Point reverseTopLeftBase = projection.projectLocation(topLeftByPixel.getLatitude(), topLeftByPixel.getLongitude());
        Assert.assertEquals(121.5625, reverseTopLeftBase.x, 0.1);
        Assert.assertEquals(82.53125, reverseTopLeftBase.y, 0.1);

        Point reverseTopLeft = projection.getPoint(topLeftByPixel.getLatitude(), topLeftByPixel.getLongitude());
        Assert.assertEquals(0, reverseTopLeft.x, 0.001);
        Assert.assertEquals(0, reverseTopLeft.y, 0.001);

        Location bottomRightLocation = projection.getLocation(areaWidth, areaHeight);
        Point bottomRight = projection.getPoint(bottomRightLocation.getLatitude(), bottomRightLocation.getLongitude());
        Assert.assertEquals(areaWidth, bottomRight.x, 0.001);
        Assert.assertEquals(areaHeight, bottomRight.y, 0.001);
    }

}

(たとえば)航空写真を扱っている場合、アルゴリズムはメルカトル図法のストレッチ効果を考慮していないように感じます。そのため、関心のある領域が赤道に比較的近くない場合、精度が低下する可能性があります。x座標に中心のcos(緯度)を掛けることで近似できると思いますか?

于 2017-01-07T23:58:15.533 に答える