gpt4 book ai didi

android - 如何将 GeoPoint 转换为硬件屏幕点

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:12:44 26 4
gpt4 key购买 nike

我想将 GeoPoint 转换为屏幕点,以便识别触摸事件上方的所有对象。所以,我试过这个:

Projection projection = this.mapView.getProjection(); 
GeoPoint gie = new GeoPoint(lat, lon);
Point po = new Point();
projection.toPixels(gie, po);

但是,po.x 和 po.y 不是屏幕坐标,而是以像素为单位的 map View 坐标,而不是纬度、经度。

来自安卓开发者网站:

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.

那么,是否可以将其转换为正确的屏幕坐标?

我想知道 + 触摸事件旁边的所有 x GeoPoint,如上例所示:

----------------------------------------------------------------
(0,0) -----------> x |
| |
| |
| | <-- My screen
| + (touch event) |
\/ x (my GeoPoint) |
y |
|
----------------------------------------------------------------

我得到了这样的触摸事件:

@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();

这里,在这段代码中,x 和 y 是真实的屏幕坐标(硬件坐标,不是 map View 坐标)

我知道我也可以转换 GeoPoint 中的 x,y 屏幕坐标,以便将它们与我的 GeoPoint 进行比较,但是,由于缩放级别,我无法获得我想要的结果。

最佳答案

我不知道您为什么要使用 onTouchEvent,它用于 View 未处理的事件。如果您使用 onTouch(View v0, MotionEvent e),这将为您提供与 View 相关的信息,您可以使用 View 的 getLeft 和 getBottom 方法计算屏幕坐标。如果您使用占据屏幕右下角的 map View 制作项目,例如:

主.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="220dp"
android:layout_height="220dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:apiKey="Your API key"
android:clickable="true" />
</RelativeLayout>

然后运行这段代码

public class GoogleMapsTouchActivity extends MapActivity implements OnTouchListener {

private MapController mMapCtrlr;
private MapView mMapVw;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onResume() {
super.onResume();
mMapVw = (MapView) findViewById(R.id.map);
mMapVw.setReticleDrawMode(MapView.ReticleDrawMode.DRAW_RETICLE_OVER);
mMapCtrlr = mMapVw.getController();
mMapCtrlr.setZoom(15);
mMapCtrlr.setCenter(new GeoPoint(53500000, -3000000));
mMapVw.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v0, MotionEvent e) {
// Only fires if Mapview touched
float x = e.getX(); // relative to VIEW
float y = e.getY(); // relative to VIEW
float x2; // relative to SCREEN
float y2; // relative to SCREEN
if (e.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(this, "View x = " + x + " View y = " + y, Toast.LENGTH_SHORT).show();
x2 = mMapVw.getLeft() + x;
y2 = mMapVw.getBottom() + y;
Toast.makeText(this, "Screen x = " + x2 + " Screen y = " + y2, Toast.LENGTH_SHORT).show();
GeoPoint gPt = mMapVw.getProjection().fromPixels((int) x, (int)y);
Toast.makeText(this, "Lat = " + gPt.getLatitudeE6() + " Lon = " + gPt.getLongitudeE6(), Toast.LENGTH_SHORT).show();

}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent e) {
// Only fires if screen touched outside a view
float x = e.getX();
float y = e.getY();
if (e.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(this, "Got touch EVENT x = " + x + " y = " + y, Toast.LENGTH_SHORT).show();
}
return super.onTouchEvent(e);
}
@Override
protected boolean isRouteDisplayed() {return false;}
}

如果您单击 View 和 map View 之外的区域,Toast 消息应该会让您一清二楚。

关于android - 如何将 GeoPoint 转换为硬件屏幕点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10685408/

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