2011-05-18 80 views
3

在我的應用程序中,我試圖在用戶滾動地圖時獲取地圖的中心座標。在Android中獲取地圖的中心座標Mapview

我想獲得座標並將其設置在文本視圖上。

這裏是我的代碼爲:

public boolean onTouchEvent(MotionEvent event) { 
     int action=event.getAction(); 
     projection=mapView.getProjection(); 
     int X = (int)event.getX();   
     int Y = (int)event.getY(); 
     if(action==MotionEvent.ACTION_MOVE) 
     { 


      metrics = new DisplayMetrics(); 
      getWindowManager().getDefaultDisplay().getMetrics(metrics); 

      GeoPoint G = projection.fromPixels(metrics.heightPixels/2, metrics.widthPixels/2); 
     //GeoPoint p= mapView.getMapCenter(); 
      int lati=p.getLatitudeE6(); 
      Log.i("Lat : ",""+lati); 
      Toast.makeText(this,""+lati,Toast.LENGTH_LONG); 

      int longi=p.getLongitudeE6(); 
      Log.i("Lon : ",""+longi); 
      Toast.makeText(this,""+longi,Toast.LENGTH_LONG); 
      lat.setText(""+lati); 
      lon.setText(""+longi); 
     } 
     return true; 
    } 
+0

你能解釋什麼不工作嗎? – 2011-05-25 11:32:19

回答

4

使用此文章:

http://mobiforge.com/developing/story/using-google-maps-android

它會回答您的所有關於使用MapView的問題(主要是全部)。加上您的特殊需求:

在頁面上搜索「獲取被觸摸的位置」。

+1

你發現如何做到這一點?我正試圖達到同樣的目的。謝謝 – Thiago 2011-08-25 18:42:58

+1

@Thiago這個問題已經更新了答案。類似於我使用的: projection = mapView.getProjection(); GeoPoint coordinatesOfMapCenter = projection.fromPixels(mapView.getWidth()/ 2,mapView.getHeight()/ 2); – 2012-05-11 21:39:26

2

OP在他的問題中包含了正確的答案。這是怎麼做到的:

GeoPoint mapCenter = mapView.getProjection().fromPixels(
     mapView.getWidth()/2, 
     mapView.getHeight()/2); 

int lat = mapCenter.getLatitudeE6(); 
int lon = mapCenter.getLongitudeE6();