【问题标题】:toPixels() returns the pixels of the tiles, instead of the screentoPixels() 返回图块的像素,而不是屏幕
【发布时间】:2012-03-28 23:50:28
【问题描述】:

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

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 来回获取屏幕像素?

【问题讨论】:

    标签: android google-maps map osmdroid


    【解决方案1】:

    你需要补偿左上角的偏移,这些方法应该可以工作:

    /**
     * 
     * @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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 2020-08-05
      • 2021-07-12
      • 2015-06-20
      • 1970-01-01
      • 2013-08-15
      相关资源
      最近更新 更多