在LatLngBounds构建器上设置最大缩放级别

Ali*_*lin 26 android zooming android-maps-v2

我的搜索中没有找到答案,有几个答案,但它们对我不起作用.

我在地图上有2个标记,我正在使用LatLngBounds构建器,以便让相机缩放到正确的缩放级别以包含它们.一切都按预期工作,除了一件事,当2个标记真的很接近时,地图非常非常放大,很好,这种缩放水平没有任何意义.

LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(firstMarker.getPosition());
builder.include(secondMarker.getPosition());
LatLngBounds bounds = builder.build();

CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, markerPadding);
Run Code Online (Sandbox Code Playgroud)

有没有办法强制一定程度的变焦,之后相机不会变焦?这样可以避免地图放大/缩小.基本上我使用15.0f作为缩放级别.如果点太远,我希望变焦适合它们.如果点越来越近,我不希望缩放级别超过15.0f.

use*_*624 44

我有一个类似的情况,我想要至少2公里的地图对角线.因此,我只是在建造者身上增加了两个点:原始边界中心西北面的前1公里和中心东南面的第二个1公里.如果它们在界限内,它们不会改变任何东西.如果它们超出原始边界,则会将边界增加到有意义的大小.

这是一个完整的代码示例:

public LatLngBounds createBoundsWithMinDiagonal(MarkerOptions firstMarker, MarkerOptions secondMarker) {
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    builder.include(firstMarker.getPosition());
    builder.include(secondMarker.getPosition());

    LatLngBounds tmpBounds = builder.build();
    /** Add 2 points 1000m northEast and southWest of the center.
     * They increase the bounds only, if they are not already larger
     * than this. 
     * 1000m on the diagonal translates into about 709m to each direction. */
    LatLng center = tmpBounds.getCenter();
    LatLng northEast = move(center, 709, 709);
    LatLng southWest = move(center, -709, -709);
    builder.include(southWest);
    builder.include(northEast);
    return builder.build();     
}

private static final double EARTHRADIUS = 6366198;
/**
 * Create a new LatLng which lies toNorth meters north and toEast meters
 * east of startLL
 */
private static LatLng move(LatLng startLL, double toNorth, double toEast) {
    double lonDiff = meterToLongitude(toEast, startLL.latitude);
    double latDiff = meterToLatitude(toNorth);
    return new LatLng(startLL.latitude + latDiff, startLL.longitude
            + lonDiff);
}

private static double meterToLongitude(double meterToEast, double latitude) {
    double latArc = Math.toRadians(latitude);
    double radius = Math.cos(latArc) * EARTHRADIUS;
    double rad = meterToEast / radius;
    return Math.toDegrees(rad);
}


private static double meterToLatitude(double meterToNorth) {
    double rad = meterToNorth / EARTHRADIUS;
    return Math.toDegrees(rad);
}
Run Code Online (Sandbox Code Playgroud)


小智 10

我根据user2808624答案实现这一解决方案,但使用SphericalUtilAndroid地图实用程序库做计算.

final double HEADING_NORTH_EAST = 45;
final double HEADING_SOUTH_WEST = 215;
LatLng center = tmpBounds.getCenter();
LatLng norhtEast = SphericalUtil.computeOffset(center, 709,HEADING_NORTH_EAST);
LatLng southWest = SphericalUtil.computeOffset(center, 709,HEADING_SOUTH_WEST );
Run Code Online (Sandbox Code Playgroud)


Bjö*_*hel 6

来晚了,但也许有人和我有同样的问题:我从一个地方收到了界限,而不是从 LatLngBounds.Builder:

我使用 Location.distanceBetween(...) 找出边界是否太小,然后使用边界中心的最小缩放级别:

if (areBoundsTooSmall(bounds, 300)) {
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(bounds.getCenter(), 17));
} else {
    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));
}
Run Code Online (Sandbox Code Playgroud)

检查方法是:

private boolean areBoundsTooSmall(LatLngBounds bounds, int minDistanceInMeter) {
    float[] result = new float[1];
    Location.distanceBetween(bounds.southwest.latitude, bounds.southwest.longitude, bounds.northeast.latitude, bounds.northeast.longitude, result);
    return result[0] < minDistanceInMeter;
}
Run Code Online (Sandbox Code Playgroud)

对于太小的边界,我使用了最小缩放级别 17 和填充 20,这里通过最小距离以米 300 定义太小。您可以根据需要调整数字。


Mar*_*arc 5

这是基于user2808624答案的更短的版本。

        LatLngBounds bounds = builder.build();
        LatLng center = bounds.getCenter();
        builder.include(new LatLng(center.latitude-0.001f,center.longitude-0.001f));
        builder.include(new LatLng(center.latitude+0.001f,center.longitude+0.001f));
        bounds = builder.build();
Run Code Online (Sandbox Code Playgroud)

有点马虎,但是你可以用0.001缩放不低于一个城市街区,0.01缩放不低于一个城市景观。唯一的优势是它只有 4 行额外的代码。