Android GMaps v2 - 获取地图中心的位置和地图的高度和宽度

use*_*397 0 java android google-maps-api-2 google-maps-android-api-2 android-maps-v2

我正在尝试在Google Maps V2 API中获取地图中心的纬度和经度,并获取地图的宽度和长度.

这是我的旧代码(来自我开始修改的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;
} 
Run Code Online (Sandbox Code Playgroud)

如何使用Android GMaps V2 API执行与mapView.getMapCenter(),mapView.getHeight()和mapView.getWidth()相同的功能?

zbr*_*zbr 10

获得中心:

GoogleMap map;
LatLng center = map.getCameraPosition().target;
Run Code Online (Sandbox Code Playgroud)

获得宽度和高度:

如果你要添加MapFragment到你的Activity使用a FragmentTransaction,你必须提供一些ViewGroup来放入片段.像一个LinearLayout或那样的东西.呼唤getWidth()getHeight()对等ViewGroup应该得到你想要的东西.

希望能帮助到你.如果您需要一个例子,请告诉我.

编辑 - 添加一个例子:

请查看Google Maps Android API v2,尤其是在名为" 添加片段"的部分.

在以开始的部分下您还可以在代码中向Activity添加MapFragment,这里有一个示例.在那个例子中,R.id.my_container正是ViewGroup我上面写的.在你的XML布局的某个地方Activity,有这样的东西:

<LinearLayout
    android:id="@+id/my_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

FragmentTransaction它的作用是什么,MapFragment并把它作为一个孩子LinearLayout.唯一的孩子.因此地图的宽度和高度与LinearLayout.

然后,您可以轻松地获得您所需要的内容Activity.

LinearLayout myContainer = (LinearLayout) findViewById(R.id.my_container);
int mapHeight = myContainer.getHeight();
int mapWidth = myContainer.getWidth();
Run Code Online (Sandbox Code Playgroud)

我希望这是可以理解的.