2012-03-12 8 views
2

OSMDroidを使用して地図を表示するAndroidアプリケーションがあります。 タイルにではなく、GeoPointの投影ピクセルを画面上に表示したいと考えています。 次のコードを考えてみましょう:toPixels()は、画面の代わりにタイルのピクセルを返します。

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); 

私はgeoPoint1geoPoint2に等しくなるようにしたいと思います。代わりに、2つの全く異なるGeoPointを取得します。私opininionで は、問題はこのラインである:

projection.toPixels(geoPoint1, pixelsPoint); 

pixelsPointアウト変数は、画面寸法(私はxとyのために10,000取得)よりもはるかに高い値で満たされますし、私はこれがあると思われます画面のピクセルではなく、タイル上のピクセル。

GeoPointからピクセルを前後にスクリーンするにはどうすればよいですか?

あなたはオフセット左上を補償する必要がある

答えて

6

、これらの方法では動作するはずです:

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