2012-05-21 10 views
6

我想一個GeoPoint轉換爲屏幕點,以確定哪些是上面的觸摸事件的所有對象。 所以,我已經試過這樣:如何一個GeoPoint轉換成硬屏點

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

但是,po.x和po.y不在屏幕COORDS,但在像素,而不是緯度,經度的MapView COORDS。

從Android開發者網站:

toPixels(GeoPoint對象中,android.graphics.Point出) 給定的GeoPoint到屏幕上的像素座標,相對於轉換左上角所提供這個的MapView的投影。

那麼,是不是可以將其轉換成正確的屏幕COORDS?

我想知道所有的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是真正的屏幕座標(硬件座標,而不是地圖視圖座標)

我知道,我也可以轉換的x,y屏幕座標的GeoPoint他們比較我的GeoPoint,但由於縮放級別的,我無法得到我想要的東西。

回答

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 
2

我不知道爲什麼你正在使用的onTouchEvent,這是不是由視圖處理的事件。如果您使用onTouch(查看V0,MotionEvent五),這會給你相對於視圖的信息,就可以計算出屏幕使用視圖的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;} 
} 

和在mapview之外的區域,Toast消息應該讓你明白。

+0

謝謝@nickt,會得到一個看你的代碼,但到了最後,這是不是我的願望。因爲我想將對象座標轉換爲屏幕座標。我有一輛車(lat,lon)--->汽車(GeoPoint)--->汽車(Point on MapView),我最後要---- ----汽車(平板電腦屏幕的x和y) – Bibu

+0

那麼觸摸事件與什麼有關呢?爲什麼混淆這個問題?如果您知道如何獲得相對於mapview原點的x和y,那麼只需添加view.getLeft和view.getBottom即可獲得相對於屏幕原點的x/y,如引用的代碼中所示。 – NickT

+0

其實,我觸摸屏幕來識別我碰過哪輛車。我所有的汽車都有經緯度座標。所以,如果我想要確定哪個車在觸摸事件旁邊(可以有多個車在它上面),我需要知道相對於屏幕分辨率的汽車位置。如果我將觸摸事件轉換爲mapview,我將獲得經緯度位置,而汽車和觸摸事件之間的距離取決於縮放級別。所以對我來說,與屏幕位置進行比較是最簡單的。希望我的信息足夠清楚。 – Bibu