2

我有一个使用 OSMDroid 显示地图的 Android 应用程序。我想GeoPoint在屏幕上而不是在瓷砖上获得 a 的投影像素。考虑以下代码:

Projection projection = getProjection();
GeoPoint geoPoint1 = (GeoPoint)projection.fromPixels(0, 0);  
Point pixelsPoint = new Point();
projection.toPixels(geoPoint1, pixelsPoint);
GeoPoint geoPoint2 = (GeoPoint)projection.fromPixels(pixelsPoint.x, pixelsPoint.y);

我想geoPoint1等于geoPoint2。相反,我得到了 2 个完全不同的“GeoPoint”。在我看来,问题出在这一行:

projection.toPixels(geoPoint1, pixelsPoint);

out 变量pixelsPoint填充的值远高于屏幕尺寸(x 和 y 为 10,000+),我怀疑这是图块上的像素,而不是屏幕像素。

如何GeoPoint来回从屏幕像素到屏幕像素?

4

1 回答 1

7

您需要补偿左上角的偏移,这些方法应该有效:

/**
 * 
 * @param x  view coord relative to left
 * @param y  view coord relative to top
 * @param vw MapView
 * @return GeoPoint
 */

private GeoPoint geoPointFromScreenCoords(int x, int y, MapView vw){
    if (x < 0 || y < 0 || x > vw.getWidth() || y > vw.getHeight()){
        return null; // coord out of bounds
    }
    // Get the top left GeoPoint
    Projection projection = vw.getProjection();
    GeoPoint geoPointTopLeft = (GeoPoint) projection.fromPixels(0, 0);
    Point topLeftPoint = new Point();
    // Get the top left Point (includes osmdroid offsets)
    projection.toPixels(geoPointTopLeft, topLeftPoint);
    // get the GeoPoint of any point on screen 
    GeoPoint rtnGeoPoint = (GeoPoint) projection.fromPixels(x, y);
    return rtnGeoPoint;
}

/**
 * 
 * @param gp GeoPoint
 * @param vw Mapview
 * @return a 'Point' in screen coords relative to top left
 */

private Point pointFromGeoPoint(GeoPoint gp, MapView vw){

    Point rtnPoint = new Point();
    Projection projection = vw.getProjection();
    projection.toPixels(gp, rtnPoint);
    // Get the top left GeoPoint
    GeoPoint geoPointTopLeft = (GeoPoint) projection.fromPixels(0, 0);
    Point topLeftPoint = new Point();
    // Get the top left Point (includes osmdroid offsets)
    projection.toPixels(geoPointTopLeft, topLeftPoint);
    rtnPoint.x-= topLeftPoint.x; // remove offsets
    rtnPoint.y-= topLeftPoint.y;
    if (rtnPoint.x > vw.getWidth() || rtnPoint.y > vw.getHeight() || 
            rtnPoint.x < 0 || rtnPoint.y < 0){
        return null; // gp must be off the screen
    }
    return rtnPoint;
}
于 2012-03-15T15:09:07.937 回答