我想将GeoPoint转换为屏幕点,以便识别触摸事件之上的所有对象。 所以,我试过这个:
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。
来自android开发者网站:
toPixels(GeoPoint in,android.graphics.Point out) 将给定的GeoPoint转换为相对于提供此投影的MapView左上角的屏幕像素坐标。
那么,是否可以在正确的屏幕上转换它?
我想知道 + 触摸事件旁边的所有 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是真正的屏幕坐标(硬件坐标,而不是mapview);
我知道我也可以在GeoPoint中转换x,y屏幕坐标以将它们与我的GeoPoint进行比较,但是,由于缩放级别,我无法得到我想要的。
答案 0 :(得分:2)
我不知道你为什么使用onTouchEvent,这是一个未被视图处理的事件。如果使用onTouch(View v0,MotionEvent e),这将为您提供与视图相关的信息,您可以使用视图的getLeft和getBottom方法计算屏幕坐标。如果您使用mapview占据屏幕右下方的项目,如:
main.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;}
}
如果您点击视图和地图视图外的区域,Toast消息应该让您清楚。
答案 1 :(得分:1)
我找到了一个解决方案,我不知道这是否是获得它的最佳方式,但它确实有效!
我感谢你的帮助@nickt,方法 getScreenRect() ,它以屏幕坐标返回屏幕的当前界限。
这里是代码:
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