gpt4 book ai didi

android - toPixels() 返回图 block 的像素,而不是屏幕

转载 作者:太空狗 更新时间:2023-10-29 16:07:54 24 4
gpt4 key购买 nike

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

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 填充的值远高于屏幕尺寸(我得到 10,000+ x 和 y),我怀疑这是图 block 上的像素,而不是屏幕像素。

如何从 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;
}

关于android - toPixels() 返回图 block 的像素,而不是屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9669455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com