0

我試圖獲取地圖中心的經緯度,在Google Maps V2 API &中獲取寬度和長度地圖。Android GMaps v2 - 獲取地圖中心的位置和地圖的高度和寬度

這裏是我的舊代碼(從GMaps V1,我開始修改):

import com.google.android.gms.maps.GoogleMap; 

private GoogleMap mGoogleMapView; 

public LatLng getMapCenter() 
{ 
    if(mGoogleMapView != null) 
    { 
    return mGoogleMapView.getMapCenter(); 
    } 
    /*if(mOpenStreetMapView != null) 
    { 
    return convertOSMGeoPoint(mOpenStreetMapView.getMapCenter()); 
    }*/ 
    return null; 
} 

public int getHeight() 
{ 
    if(mGoogleMapView != null) 
    { 
    return mGoogleMapView.getHeight(); 
    }  
    /*if(mOpenStreetMapView != null) 
    { 
    return mOpenStreetMapView.getHeight(); 
    }*/ 
    return 0; 
} 

public int getWidth() 
{ 
    if(mGoogleMapView != null) 
    { 
    return mGoogleMapView.getWidth(); 
    } 
    /*else if(mOpenStreetMapView != null) 
    { 
    return mOpenStreetMapView.getWidth(); 
    }*/ 
    return 0; 
} 

我怎麼能執行相同的功能,mapView.getMapCenter(),mapView.getHeight(),和MapView的。 getWidth()使用Android GMaps V2 API?

回答

10

獲取中心:

GoogleMap map; 
LatLng center = map.getCameraPosition().target; 

獲取寬度和高度:

如果您使用的是FragmentTransaction添加MapFragmentActivity,你必須提供一些ViewGroup把片段插入。像LinearLayout或類似的東西。撥打getWidth()getHeight()這樣ViewGroup應該得到你想要的。

希望它有幫助。如果你需要一個例子,讓我知道。

編輯 - 添加一個例子:

Google Maps Android API v2看看,特別是在命名的部分:Add a Fragment

在以開頭的部分下您還可以將代碼中的MapFragment添加到活動中,有一個示例。在那個例子中,R.id.my_container就是我上面寫的ViewGroup。在某處,Activity的XML佈局,也有一些是這樣的:

<LinearLayout 
    android:id="@+id/my_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

什麼FragmentTransaction所做的是,它需要一個MapFragment,並把它作爲這個LinearLayout的孩子。唯一的孩子。因此,該地圖的寬度和高度與LinearLayout相同。

然後,你可以很容易地得到你需要在你的Activity這樣的。

LinearLayout myContainer = (LinearLayout) findViewById(R.id.my_container); 
int mapHeight = myContainer.getHeight(); 
int mapWidth = myContainer.getWidth(); 

我希望這是可以理解的。

+0

如果你能提供一個很好的例子。 – user268397 2013-05-03 01:10:55

+0

請檢查我對答案的編輯。 – zbr 2013-05-03 01:33:46

相關問題