Android谷歌地图:如何获取当前显示在屏幕设备中的区域?

时间:2013-04-17 09:27:21

标签: android google-maps coordinates

有没有办法获取当前区域的坐标,该坐标显示在设备上?

背景是,我们想要显示“附近”的地方,这些地方存储在我们自己的数据库中。因此,假设用户查看地图的以下剪辑:

enter image description here

我们如何获得屏幕的经度/纬度(或屏幕中间的点和覆盖所有内容的半径?)。请记住,地图中心通常不是当前位置,因为用户可以移动卡的中心!

3 个答案:

答案 0 :(得分:33)

使用map.getProjection().getVisibleRegion()。从VisibleRegion开始,您可以获得LatLngBounds,这很容易使用。您也可以直接尝试使用可能是梯形的区域。

答案 1 :(得分:10)

我从几个回复中找到了Google Map API v2的解决方案:

stackoverflow#1stackoverflow#2

因此,需要实现GoogleMap.OnCameraChangeListener接口

中的Activity

    private static final int REQUEST_CODE_GOOGLE_PLAY_SERVECES_ERROR = -1;
    private static final double EARTH_RADIOUS = 3958.75; // Earth radius;
    private static final int METER_CONVERSION = 1609;
    private GoogleMap mGoogleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);

        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
        if (status != ConnectionResult.SUCCESS)
        {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, activity,
                    REQUEST_CODE_GOOGLE_PLAY_SERVECES_ERROR);
            dialog.show();
            mGoogleMap = null;
        }
        else
        {
            mGoogleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(
                    R.id.fragment_shops_layout_maps_fragment)).getMap();

            mGoogleMap.setOnCameraChangeListener(this);
        }
    }

侦听器,在缩放地图时工作。将LatLng确定为地图左下角,右下角,左上角和右上角的位置,在屏幕上显示。通过屏幕的最大边和两个点,我们可以从地图中心获得半径。

    @Override
    public void onCameraChange(CameraPosition cameraPosition)
    {
        // Listener of zooming;
        float zoomLevel = cameraPosition.zoom;
        VisibleRegion visibleRegion = mGoogleMap.getProjection().getVisibleRegion();
        LatLng nearLeft = visibleRegion.nearLeft;
        LatLng nearRight = visibleRegion.nearRight;
        LatLng farLeft = visibleRegion.farLeft;
        LatLng farRight = visibleRegion.farRight;
        double dist_w = distanceFrom(nearLeft.latitude, nearLeft.longitude, nearRight.latitude, nearRight.longitude);
        double dist_h = distanceFrom(farLeft.latitude, farLeft.longitude, farRight.latitude, farRight.longitude);
        Log.d("DISTANCE: ", "DISTANCE WIDTH: " + dist_w + " DISTANCE HEIGHT: " + dist_h);
    }

两点之间的返回距离,以米为单位存储为2对位置;

    public double distanceFrom(double lat1, double lng1, double lat2, double lng2)
    {
        // Return distance between 2 points, stored as 2 pair location;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLng = Math.toRadians(lng2 - lng1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double dist = EARTH_RADIOUS * c;
        return new Double(dist * METER_CONVERSION).floatValue();
    }

如果你想获得面积半径,屏幕上显示的只需要除以2。

我希望有用!

答案 2 :(得分:2)

这将根据地图宽度计算以km为单位的无线电:

public double calculateVisibleRadius() {
    float[] distanceWidth = new float[1];
    VisibleRegion visibleRegion = map.getProjection().getVisibleRegion();
    LatLng farRight = visibleRegion.farRight;
    LatLng farLeft = visibleRegion.farLeft;
    LatLng nearRight = visibleRegion.nearRight;
    LatLng nearLeft = visibleRegion.nearLeft;
    //calculate the distance between left <-> right of map on screen
    Location.distanceBetween( (farLeft.latitude + nearLeft.latitude) / 2, farLeft.longitude, (farRight.latitude + nearRight.latitude) / 2, farRight.longitude, distanceWidth );
    // visible radius is / 2  and /1000 in Km:
     return distanceWidth[0] / 2 / 1000 ;
}