2013-01-19 66 views
0

我想要獲取緯度和經度的地圖中心(谷歌地圖api v2)顯示在按鈕按下textview。但它不斷崩潰。我嘗試了幾件事情,這是對我來說最好的,但是一旦你按下按鈕就會崩潰:獲取地圖中心座標的按鈕按下,崩潰

我認爲它崩潰在這一行上:MapView mapView =(MapView)findViewById(R。 id.map); (因爲我試圖刪除一切在onclick,它仍然crahes)

的Java:

public class Main extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 
     final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 

     map.setMyLocationEnabled(true); 


     final TextView latitudeTV = (TextView) findViewById(R.id.latitude); 
     final TextView longitudeTV = (TextView) findViewById(R.id.longitude); 

     Button buttonCoor = (Button) findViewById(R.id.getCoor);  
     buttonCoor.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       MapView mapView = (MapView)findViewById(R.id.map); 
       GeoPoint mapCenter = mapView.getMapCenter(); 
       int latInt = mapCenter.getLatitudeE6(); 
       int lonInt = mapCenter.getLongitudeE6(); 
       latitudeTV.setText(latInt); 
       longitudeTV.setText(lonInt); 

      } 
     }); 

    } 

這有什麼錯我的代碼?

由於api鍵我無法在模擬器上運行它,所以無法獲取任何logcat信息。

謝謝

UPDATE:

我也嘗試過這一點,但同樣的結果(在按下按鈕崩潰)

 MapView mapView = (MapView)findViewById(R.id.map); 
    GeoPoint mapCenter = mapView.getProjection().fromPixels(
     mapView.getWidth()/2, 
     mapView.getHeight()/2); 

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

回答

0

我終於解決了花了近4個小時,哈哈!這是我結束了:

final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
... 
Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); 
int height = display.getHeight(); 
Point point = new Point(width/2, height/2); 
LatLng latLng = map.getProjection().fromScreenLocation(point); 

latitudeTV.setText(String.valueOf(latLng)); 

多虧了這傢伙的教程:

http://patesush.blogspot.dk/2012/12/how-to-create-app-for-google-map-v2-in.html