تقوم الدالة toPixels()‎ بإرجاع وحدات البكسل الموجودة في المربعات بدلاً من الشاشة

StackOverflow https://stackoverflow.com//questions/9669455

سؤال

لدي تطبيق Android يعرض الخرائط باستخدام OSMDroid.أريد الحصول على بكسلات الإسقاط لـ a 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.وبدلاً من ذلك، حصلت على نقطتين مختلفتين تمامًا من GeoPoint.في رأيي المشكلة تكمن في هذا السطر:

projection.toPixels(geoPoint1, pixelsPoint);

المتغير الخارج pixelsPoint تمتلئ بقيم أعلى بكثير من أبعاد الشاشة (أحصل على 10000+ لـ x وy) وأظن أن هذه هي وحدات البكسل الموجودة على البلاط، وليست وحدات بكسل الشاشة.

كيف يمكنني الحصول من GeoPoint لشاشة بكسل ذهابا وإيابا؟

هل كانت مفيدة؟

المحلول

تحتاج إلى تعويض الإزاحة العلوية اليسرى، ومن المفترض أن تنجح هذه الطرق:

/**
 * 
 * @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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top