如何将GeoPoint转换为硬件屏幕点 [英] How to convert a GeoPoint to a Hardware screen point
问题描述
我想将GeoPoint转换为屏幕点,以便识别触摸事件上方的所有对象. 所以,我已经尝试过了:
I would like to convert a GeoPoint to a screen point in order to identify all objects which are above a touch event. So, I've tried this :
Projection projection = this.mapView.getProjection();
GeoPoint gie = new GeoPoint(lat, lon);
Point po = new Point();
projection.toPixels(gie, po);
但是,po.x和po.y不是屏幕坐标,而是mapview坐标(以像素为单位,而不是lat,lon).
But, po.x and po.y are not the screen coords, but the mapview coords in pixel instead of lat,lon.
从android开发者网站:
From the android developer website :
toPixels(GeoPoint输入,android.graphics.Point输出) 相对于提供此Projection的MapView的左上角,将给定的GeoPoint转换为屏幕上的像素坐标.
toPixels(GeoPoint in, android.graphics.Point out) Converts the given GeoPoint to onscreen pixel coordinates, relative to the top-left of the MapView that provided this Projection.
那么,有可能将其转换为正确的屏幕坐标吗?
So, is it possible to convert it in the correct screen coords ?
我想知道 + 触摸事件旁边的所有 x GeoPoint,就像上面的示例一样:
I want to know all the x GeoPoint which are next to the + touch event like on the example above :
----------------------------------------------------------------
(0,0) -----------> x |
| |
| |
| | <-- My screen
| + (touch event) |
\/ x (my GeoPoint) |
y |
|
----------------------------------------------------------------
我得到了这样的触摸事件:
I get the touch event like that :
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
在此代码中,x和y是实际的屏幕坐标(硬件坐标,而不是mapview坐标)
Here, in this code, x and y are the real screen coords (the hardware ones, not the mapview ones)
我知道我也可以在GeoPoint中转换x,y屏幕坐标,以将它们与我的GeoPoint进行比较,但是,由于缩放级别的原因,我无法获得想要的.
I know that I can also convert x,y screen coordinates in GeoPoint to compare them to my GeoPoint, but, because of the zoom level, I can't get what I want.
推荐答案
我已经找到了解决方案,我不知道这是否是获得它的最佳方法,但是它有效!
I've found a solution, I don't know if it's the best way to get it, but it works!
我发现感谢@nickt的帮助,方法 getScreenRect() 返回屏幕坐标的当前边界.
I've found thank to your help @nickt, the method getScreenRect() which return the current bounds of the screen in screen coordinates.
代码在这里:
float pisteX;
float pisteY;
Projection projection = this.mapView.getProjection();
Point pt = new Point();
GeoPoint gie = new GeoPoint(latitude,longitude);
Rect rec = mapView.getScreenRect(new Rect());
projection.toPixels(gie, pt);
pisteX = pt.x-rec.left; // car X screen coord
pisteY = pt.y-rec.top; // car Y screen coord
这篇关于如何将GeoPoint转换为硬件屏幕点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!