简体   繁体   English

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

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

I'm trying to get the latitude and longitude of the center of the map in the Google Maps V2 API & get the width and length of the map. 我正在尝试在Google Maps V2 API中获取地图中心的纬度和经度,并获取地图的宽度和长度。

Here is my old code(from GMaps v1 that I started to modify): 这是我的旧代码(来自我开始修改的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;
} 

How can I perform the same functionality that mapView.getMapCenter(), mapView.getHeight(), and mapView.getWidth() using the Android GMaps V2 API? 如何使用Android GMaps V2 API执行与mapView.getMapCenter(),mapView.getHeight()和mapView.getWidth()相同的功能?

Getting the center: 获得中心:

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

Getting the width and height: 获取宽度和高度:

If you're adding the MapFragment to your Activity using a FragmentTransaction , you have to provide some ViewGroup to put the fragment into. 如果要使用FragmentTransactionMapFragment添加到Activity ,则必须提供一些ViewGroup来将片段放入其中。 Something like a LinearLayout or such. 诸如LinearLayout类的东西。 Calling getWidth() and getHeight() on such ViewGroup should get you what you want. 在这样的ViewGroup上调用getWidth()getHeight()应该可以为您提供所需的东西。

Hope it helps. 希望能帮助到你。 If you need an example, let me know. 如果您需要一个例子,请告诉我。

EDIT - adding an example: 编辑-添加示例:

Take a look at the Google Maps Android API v2 , especially at the section named: Add a Fragment . 看一下Google Maps Android API v2 ,尤其是在名为: Add Fragment的部分。

Under the part that starts with You can also add a MapFragment to an Activity in code , there is an example. 在开头的部分下, 您还可以在代码中将MapFragment添加到Activity中 ,下面是一个示例。 In that example, R.id.my_container is exactly the ViewGroup I wrote about above. 在该示例中, R.id.my_container正是我上面编写的ViewGroup Somewhere in the XML layout of your Activity , there is something like this: Activity的XML布局中的某处,类似以下内容:

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

What the FragmentTransaction does is that it takes a MapFragment and puts it as a child of this LinearLayout . FragmentTransaction所做的是,它接受一个MapFragment并将其作为此LinearLayout的子级。 The only child. 独生子。 Therefore the map has the same width and height as the LinearLayout . 因此,地图具有与LinearLayout相同的宽度和高度。

Then you can easily get what you need in your Activity like this. 这样,您就可以轻松地在Activity获得所需的内容。

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

I hope it's understandable. 我希望这是可以理解的。

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

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