繁体   English   中英

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

[英]Android GMaps v2 - Get location of the center of the map & map height and width

我正在尝试在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;
} 

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

获得中心:

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

获取宽度和高度:

如果要使用FragmentTransactionMapFragment添加到Activity ,则必须提供一些ViewGroup来将片段放入其中。 诸如LinearLayout类的东西。 在这样的ViewGroup上调用getWidth()getHeight()应该可以为您提供所需的东西。

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

编辑-添加示例:

看一下Google Maps Android API v2 ,尤其是在名为: Add Fragment的部分。

在开头的部分下, 您还可以在代码中将MapFragment添加到Activity中 ,下面是一个示例。 在该示例中, 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();

我希望这是可以理解的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM