谷歌地图api V2调整大小屏幕以匹配绘制的圆圈

An-*_*oid 2 java math android google-maps google-maps-android-api-2

我正在地图中绘制一个圆圈(在用户的当前位置),我希望屏幕可以缩放以使整个圆圈具有一定的余量.我现在在做什么:

drawCercleAroundPin(_googleMap, DataManager.RADIUS_SEARCH_CERCLE, _location);

moveCamera(_googleMap, (10 / ((DataManager.RADIUS_SEARCH_CERCLE / 900) + 1))+10, 2000, _location, null);
Run Code Online (Sandbox Code Playgroud)

好吧,我尝试了一些愚蠢的微积分,因为我无法找到合适的解决方案......

有没有人有想法?

var*_*run 6

如果我理解正确,您希望地图适合圆形边界?如果是这样

这样添加一个圆圈

mMap.addCircle(new CircleOptions()
        .center(new LatLng(location.getLatitude(), location.getLongitude()))
        .radius(100)
        .strokeColor(Color.RED)
        .fillColor(Color.BLUE));
Run Code Online (Sandbox Code Playgroud)

**那么你需要你的圈子的边框,阅读这个**

LatLngBounds bounds = boundsWithCenterAndLatLngDistance(new LatLng(location.getLatitude(), location.getLongitude()),200,200);
Run Code Online (Sandbox Code Playgroud)

**并尝试**

private static final double ASSUMED_INIT_LATLNG_DIFF = 1.0;
private static final float ACCURACY = 0.01f;

public static LatLngBounds boundsWithCenterAndLatLngDistance(LatLng center, float latDistanceInMeters, float lngDistanceInMeters) {
    latDistanceInMeters /= 2;
    lngDistanceInMeters /= 2;
    LatLngBounds.Builder builder = LatLngBounds.builder();
    float[] distance = new float[1];
    {
        boolean foundMax = false;
        double foundMinLngDiff = 0;
        double assumedLngDiff = ASSUMED_INIT_LATLNG_DIFF;
        do {
            Location.distanceBetween(center.latitude, center.longitude, center.latitude, center.longitude + assumedLngDiff, distance);
            float distanceDiff = distance[0] - lngDistanceInMeters;
            if (distanceDiff < 0) {
                if (!foundMax) {
                    foundMinLngDiff = assumedLngDiff;
                    assumedLngDiff *= 2;
                } else {
                    double tmp = assumedLngDiff;
                    assumedLngDiff += (assumedLngDiff - foundMinLngDiff) / 2;
                    foundMinLngDiff = tmp;
                }
            } else {
                assumedLngDiff -= (assumedLngDiff - foundMinLngDiff) / 2;
                foundMax = true;
            }
        } while (Math.abs(distance[0] - lngDistanceInMeters) > lngDistanceInMeters * ACCURACY);
        LatLng east = new LatLng(center.latitude, center.longitude + assumedLngDiff);
        builder.include(east);
        LatLng west = new LatLng(center.latitude, center.longitude - assumedLngDiff);
        builder.include(west);
    }
    {
        boolean foundMax = false;
        double foundMinLatDiff = 0;
        double assumedLatDiffNorth = ASSUMED_INIT_LATLNG_DIFF;
        do {
            Location.distanceBetween(center.latitude, center.longitude, center.latitude + assumedLatDiffNorth, center.longitude, distance);
            float distanceDiff = distance[0] - latDistanceInMeters;
            if (distanceDiff < 0) {
                if (!foundMax) {
                    foundMinLatDiff = assumedLatDiffNorth;
                    assumedLatDiffNorth *= 2;
                } else {
                    double tmp = assumedLatDiffNorth;
                    assumedLatDiffNorth += (assumedLatDiffNorth - foundMinLatDiff) / 2;
                    foundMinLatDiff = tmp;
                }
            } else {
                assumedLatDiffNorth -= (assumedLatDiffNorth - foundMinLatDiff) / 2;
                foundMax = true;
            }
        } while (Math.abs(distance[0] - latDistanceInMeters) > latDistanceInMeters * ACCURACY);
        LatLng north = new LatLng(center.latitude + assumedLatDiffNorth, center.longitude);
        builder.include(north);
    }
    {
        boolean foundMax = false;
        double foundMinLatDiff = 0;
        double assumedLatDiffSouth = ASSUMED_INIT_LATLNG_DIFF;
        do {
            Location.distanceBetween(center.latitude, center.longitude, center.latitude - assumedLatDiffSouth, center.longitude, distance);
            float distanceDiff = distance[0] - latDistanceInMeters;
            if (distanceDiff < 0) {
                if (!foundMax) {
                    foundMinLatDiff = assumedLatDiffSouth;
                    assumedLatDiffSouth *= 2;
                } else {
                    double tmp = assumedLatDiffSouth;
                    assumedLatDiffSouth += (assumedLatDiffSouth - foundMinLatDiff) / 2;
                    foundMinLatDiff = tmp;
                }
            } else {
                assumedLatDiffSouth -= (assumedLatDiffSouth - foundMinLatDiff) / 2;
                foundMax = true;
            }
        } while (Math.abs(distance[0] - latDistanceInMeters) > latDistanceInMeters * ACCURACY);
        LatLng south = new LatLng(center.latitude - assumedLatDiffSouth, center.longitude);
        builder.include(south);
    }
    return builder.build();
}
Run Code Online (Sandbox Code Playgroud)

最后打电话

CameraUpdateFactory#newLatLngBounds(bounds, padding);
Run Code Online (Sandbox Code Playgroud)

  • 我觉得从其他答案复制代码是不好的,并且链接到原始帖子要好得多.我打算重构这段代码,因为它不是最理想的,也需要在这里重构它.无论如何,我还修改了代码用法,你发送半径(100米)而不是直径(200米).最初的问题是关于显示由中心,宽度和高度定义的区域.对于圆宽和高度等于2*半径.对于任何感兴趣的人,这是原始答案:http://stackoverflow.com/questions/6224671/mkcoordinateregionmakewithdistance-equvivalent-in-android/16417396#16417396 (4认同)