Android 谷歌地图为缩放级别获取可见区域半径

Android 谷歌地图为缩放级别获取可见区域半径,android,google-maps,coordinates,geography,Android,Google Maps,Coordinates,Geography,我正在构建一个应用程序,从onCameraIdle中的API加载数据。请求需要目标位置和半径,以提供结果 我用于从当前摄影机获取半径的代码是: /** * @param map The Map on which to compute the radius. * @return The circle radius of the area visible on map, in meters. */ public float getRadiusVisibleOnMap(GoogleMap map

我正在构建一个应用程序,从
onCameraIdle
中的API加载数据。请求需要目标位置半径,以提供结果

我用于从当前摄影机获取半径的代码是:

/**
 * @param map The Map on which to compute the radius.
 * @return The circle radius of the area visible on map, in meters.
 */
public float getRadiusVisibleOnMap(GoogleMap map) {
    VisibleRegion visibleRegion = map.getProjection().getVisibleRegion();

    LatLng farRight = visibleRegion.farRight;
    LatLng farLeft = visibleRegion.farLeft;
    LatLng nearRight = visibleRegion.nearRight;
    LatLng nearLeft = visibleRegion.nearLeft;

    float[] distanceWidth = new float[1];
    Location.distanceBetween(
            (farRight.latitude + nearRight.latitude) / 2,
            (farRight.longitude + nearRight.longitude) / 2,
            (farLeft.latitude + nearLeft.latitude) / 2,
            (farLeft.longitude + nearLeft.longitude) / 2,
            distanceWidth
    );


    float[] distanceHeight = new float[1];
    Location.distanceBetween(
            (farRight.latitude + nearRight.latitude) / 2,
            (farRight.longitude + nearRight.longitude) / 2,
            (farLeft.latitude + nearLeft.latitude) / 2,
            (farLeft.longitude + nearLeft.longitude) / 2,
            distanceHeight
    );

    float radius;

    if (distanceWidth[0] > distanceHeight[0]) {
        radius = distanceWidth[0];
    } else {
        radius = distanceHeight[0];
    }

    return radius;
}
我是从你那儿得到的

在某些情况下,我需要仅知道缩放级别来获取半径,以便在缩放动画完成时信息已加载到地图上

我试着从调整代码以获得知道缩放级别的半径,而不是相反

public int getZoomLevel(Circle circle) {
    if (circle != null) {
        double radius = circle.getRadius();
        double scale = radius / 500;
        zoomLevel = (int) (16 - Math.log(scale) / Math.log(2));
    }
    return zoomLevel;
}
我得到了公式:


这个公式不正确。它没有输出正确的尺寸。对于14级的变焦,我使用相机的可视区域获得了2.697331km的半径尺寸。如果我使用公式计算半径,结果是0.008km。

看起来最好的做法是找到一个适合内部地图矩形的最小半径值

您就快到了,请使用下面的修改函数,该函数用于查找包含地图“矩形”所需的最小半径




你现在面临的问题是什么?我得到的计算半径的公式不正确。它没有输出正确的尺寸。对于14级的变焦,我使用相机的可视区域获得了2.697331km的半径尺寸。如果我使用公式计算半径,结果是0.008km。在视觉基准上是否有多个经纬度?如果那样的话,我可以给你计算半径的逻辑。我的意思是基于多个lat/lonI的缩放我不确定你的意思。在启动以下命令之前,我需要半径:map.animateCamera(CameraUpdateFactory.newLatLngZoom(location,zoomLevel),callback);我有位置:LatLng和zoomLevel:float我的意思是,如果位置1有Lat/Lon,位置2有Lat/Lon,位置3有Lat/Lon,那么我可以给你一个公式,根据所有这些位置计算缩放
@Override
public void onMapReady(GoogleMap googleMap) {
    this.googleMap = googleMap;
    googleMap.setOnMarkerClickListener(this);

    googleMap.setOnCameraIdleListener(() -> {
        CameraPosition cameraPosition = googleMap.getCameraPosition();
        double radiusInMeters = getMapVisibleRadius();
        double radiusInKilometers = radiusInMeters / 1000;
        double latitude = cameraPosition.target.latitude;
        double longitude = cameraPosition.target.longitude;

        // TODO you take it from here, 
        // I use firebase to get relevant markers based on the lat,long,radius

    });
}


private double getMapVisibleRadius() {
    VisibleRegion visibleRegion = googleMap.getProjection().getVisibleRegion();

    float[] distanceWidth = new float[1];
    float[] distanceHeight = new float[1];

    LatLng farRight = visibleRegion.farRight;
    LatLng farLeft = visibleRegion.farLeft;
    LatLng nearRight = visibleRegion.nearRight;
    LatLng nearLeft = visibleRegion.nearLeft;

    Location.distanceBetween(
            (farLeft.latitude + nearLeft.latitude) / 2,
            farLeft.longitude,
            (farRight.latitude + nearRight.latitude) / 2,
            farRight.longitude,
            distanceWidth
    );

    Location.distanceBetween(
            farRight.latitude,
            (farRight.longitude + farLeft.longitude) / 2,
            nearRight.latitude,
            (nearRight.longitude + nearLeft.longitude) / 2,
            distanceHeight
    );

    double radiusInMeters = Math.sqrt(Math.pow(distanceWidth[0], 2) + Math.pow(distanceHeight[0], 2)) / 2;
    return radiusInMeters;
}