Gogle地图可见区域内的圆半径

时间:2019-02-18 07:19:57

标签: android google-maps

我想获取刻在屏幕上可见区域的矩形内的圆的半径...谷歌地图sdk只提供了nearLeft,farLeft,nearRight和farRight ...这样我就可以得到如下:

enter image description here

但是我需要的是:

enter image description here

我一直在使用以下代码:

    public static double getMapVisibleRadius(GoogleMap map) {
    VisibleRegion visibleBounds = map.getProjection().getVisibleRegion();
    LatLng center = visibleBounds.latLngBounds.getCenter();
    LatLng northEast = visibleBounds.nearLeft;
    // r = radius of the earth in km
    double r = 6378.8;
    // degrees to radians (divide by 57.2958)
    double ne_lat = northEast.latitude / 57.2958;
    double ne_lng = northEast.longitude / 57.2958;
    double c_lat = center.latitude / 57.2958;
    double c_lng = center.longitude / 57.2958;
    // distance = circle radius from center to Northeast corner of bounds
    double r_km = r * Math.acos(
            Math.sin(c_lat) * Math.sin(ne_lat) +
                    Math.cos(c_lat) * Math.cos(ne_lat) * Math.cos(ne_lng - c_lng)
    );
    return r_km; // radius in meters

}

在第一张图像中提供圆的半径。

我也尝试过使用从位置开始的distancebetwwen来找到farleft和nearleft的中点...我一直打算寻找地图中心之间的距离,从而找到中点。但是,我无法获得该点的信息。.所以我无法继续..

VisibleRegion visibleBounds = mGoogleMap.getProjection().getVisibleRegion();
    float[] distanceBetweentopAndBottomLeftCorners = new float[1];
    float[] distanceBetweenMiddleOfLeftAndCenterOfVisibleRegion = new float[1];
    LatLng topLeftCorner = visibleBounds.farLeft;
    LatLng bottomLeftCorner = visibleBounds.nearLeft;
    Location.distanceBetween(topLeftCorner.latitude, topLeftCorner.longitude, bottomLeftCorner.latitude, bottomLeftCorner.longitude, distanceBetweentopAndBottomLeftCorners );
    float centerOfVisibleRegionLeftToScreen = distanceBetweentopAndBottomLeftCorners [0]/2;
   // Here I am unable to proceed since the above calculated value is a float and not Latlng. My idea was to find the distance between this value and center of visible bound;

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您正在测量左上角坐标与左下角坐标之间的距离,因此它可以为您提供垂直距离(直到底部)。要测量水平距离,应使用“ farLeft and farRight”或“ nearLeft and nearRight”坐标(从左到右)。

VisibleRegion visibleBounds = mGoogleMap.getProjection().getVisibleRegion();
float[] distanceBetweenLeftAndRightCorners = new float[1];
LatLng topLeftCorner = visibleBounds.farLeft;
LatLng topRightCorner = visibleBounds.farRight;
Location.distanceBetween(topLeftCorner.latitude, topLeftCorner.longitude, topRightCorner.latitude, topRightCorner.longitude, distanceBetweenLeftAndRightCorners );
float thisIsWhatYouNeedInMeters = distanceBetweenLeftAndRightCorners [0]/2;

这样,您将在屏幕顶部或屏幕底部获得距离,但是我想所有水平距离都将相同,因此您将获得所需的东西。

如果要精确测量下面想要的坐标,可以将其传递给distanceBetween()。正如我提到的,结果不会有所不同。

LatLng leftCenter = new LatLng((visibleBounds.farLeft.latitude + visibleBounds.nearLeft.latitude)/2, (visibleBounds.farLeft.longitude + visibleBounds.nearLeft.longitude)/2);
LatLng screenCenter = visibleBounds.latLngBounds.getCenter();