【发布时间】:2015-02-17 10:48:35
【问题描述】:
我正在使用 OpenStreetMaps(使用 osmdroid)制作一个 Android 应用程序。 在此地图上,显示了标记(这些标记的经度/纬度和其他数据是从 json 文件中检索的)。这些标记是 OverlayItems 并且都在 MapView 上正确显示。这些位图的 X 和 Y 坐标是通过获取地图视图的投影来计算的,然后根据地理点的 X 和 Y 结合位图的宽度/2 和高度/2,将位图绘制在世界地图上的正确位置。
长话短说:位图的 X 和 Y 值是动态计算的。 我的问题是,我正在编写当用户在标记位图上单击(= 进行触摸事件)时应该发生的代码。为此,我需要检查事件的 X 和 Y 是否在位图的范围内。 我该怎么做?
不幸的是,似乎没有简单的 bitmap.getX 或 .getY,这基本上是我需要的。
这是绘制位图的代码(发生在@Override draw() 中),如果它可能有用的话:
for(OverlayItem p: waypointMarkers){
IGeoPoint in = p.getPoint();
Point out = new Point();
mapview.getProjection().toPixels(in, out);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.marker);
bitmaps.put(bm, p);
canvas.drawBitmap(bm,
out.x - bm.getWidth()/2, //shift the bitmap center
out.y - bm.getHeight()/2, //shift the bitmap center
null);
}
bitmaps.put(bm, p); 是一个带有 Bitmap、OverlayItem 对的 HashMap。稍后在我的代码中,我正在遍历这个 HashMap 的键,并且我想在其中检查位图的 X 和 Y。
for(Bitmap b: bitmaps.keySet()){
if (x >= xOfBitmap && x < (xOfBitmap + yourBitmap.getWidth())
&& y >= yOfBitmap && y < (yOfBitmap + yourBitmap.getHeight())) {
//found this on StackOverflow, if this is true than user has clicked somewhere on the bitmap
}
提前致谢。
【问题讨论】:
-
a HashMap with Bitmap, OverlayItem pair.。你不能从 OverlayItem 再次计算 x,y 吗?和以前一样。 -
很遗憾,OverlayItems 目前没有与位图的链接。 OverlayItems 纯粹包含带有名称等的字符串值和全球位置的地理点。我目前正在尝试使用一个包含 int X、int Y 和位图的类,该类是在绘制位图之前创建的,因此这些对象应该包含 X 和 Y。但到目前为止还没有运气,认为它有一些东西要使用 int/float 转换(世界地图上逗号后的一位数字立即完全是另一个位置)
-
对不起。我不明白为什么你不能再次计算 x,y。但如果不是,请使用 bitmaps.put(bm, p, out.x, out.y);顺便说一下,为什么需要 int/float 转换?
标签: android bitmap osmdroid android-bitmap