Android GMaps v2-获取地图中心的位置地图的高度和宽度 [英] Android GMaps v2 - Get location of the center of the map & map height and width

查看:70
本文介绍了Android GMaps v2-获取地图中心的位置地图的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的旧代码(来自我开始修改的GMaps v1):

Here is my old code(from GMaps v1 that I started to modify):

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()相同的功能?

How can I perform the same functionality that mapView.getMapCenter(), mapView.getHeight(), and mapView.getWidth() using the Android GMaps V2 API?

推荐答案

获得中心:

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

获取宽度和高度:

如果要使用 FragmentTransaction MapFragment 添加到 Activity 中,则必须提供一些 ViewGroup 将片段放入.类似于 LinearLayout 之类的东西.在这样的 ViewGroup 上调用 getWidth() getHeight()应该会为您提供所需的东西.

If you're adding the MapFragment to your Activity using a FragmentTransaction, you have to provide some ViewGroup to put the fragment into. Something like a LinearLayout or such. Calling getWidth() and getHeight() on such ViewGroup should get you what you want.

希望它会有所帮助.如果您需要一个例子,请告诉我.

Hope it helps. If you need an example, let me know.

编辑-添加示例:

看看 Google Maps Android API v2 ,特别是在名为:添加片段.

Take a look at the Google Maps Android API v2, especially at the section named: Add a Fragment.

在以开头的部分下,您还可以通过代码在活动中添加MapFragment ,下面是一个示例.在该示例中, R.id.my_container 正是我上面写过的 ViewGroup .在 Activity 的XML布局中的某处,类似以下内容:

Under the part that starts with You can also add a MapFragment to an Activity in code, there is an example. In that example, R.id.my_container is exactly the ViewGroup I wrote about above. Somewhere in the XML layout of your Activity, there is something like this:

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

FragmentTransaction 所执行的操作是,它接受一个 MapFragment ,并将其作为此 LinearLayout 的子代.独生子.因此,地图的宽度和高度与 LinearLayout 相同.

What the FragmentTransaction does is that it takes a MapFragment and puts it as a child of this LinearLayout. The only child. Therefore the map has the same width and height as the LinearLayout.

然后,您可以像这样在 Activity 中轻松获得所需的内容.

Then you can easily get what you need in your Activity like this.

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

我希望这是可以理解的.

I hope it's understandable.

这篇关于Android GMaps v2-获取地图中心的位置地图的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆