在 LatLngBounds 构建器上设置最大缩放级别 [英] Set a max zoom level on LatLngBounds builder

查看:39
本文介绍了在 LatLngBounds 构建器上设置最大缩放级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在搜索中没有找到答案,SO 上有一些答案,但它们对我不起作用.

I haven't found an answer in my search, there are a few answers on SO but they didn't work for me.

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

I have 2 markers on the map and I am using LatLngBounds builder in order to let the camera zoom to the right zoom level to include them both. Everything works as expected aside one thing, when the 2 markers are really close to each other, the map is very very zoomed and well, it doesn't make any sense to have this level of zooming.

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

CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, markerPadding);

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

Is there a way to force a certain level of zoom, after which the camera won't zoom ? This would avoid having the map too zoomed in/out. Basically I am using 15.0f as level of zoom. If the points are too far away I want the zoom to fit them both. If points are getting close I don't want the zoom level to go beyond 15.0f.

推荐答案

我有一个类似的案例,我想要至少 2 公里的地图对角线.因此,我只向构建器添加了两个额外的点:原始边界中心西北方向的第一个 1 公里和中心东南方向的第二个 1 公里.如果这些在边界内,它们不会改变任何东西.如果它们在原始边界之外,则将边界增加到有意义的大小.

I have a similar case, where I want at least a map diagonal of 2 kilometers. Therefore I just add two additional points to the builder: the first 1 km north-west of the center of the original bounds and the second 1 km south-east of the center. If these are inside the bounds, they do not change anything. If they are outside of the original bounds, they increase the bounds to a meaningful size.

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

Here is a full code example:

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);
}

编辑,因为这似乎仍然很受欢迎:

请参阅下面的 https://stackoverflow.com/a/23092644/2808624:如今人们会使用 SphericalUtil移动 LatLng 坐标,而不是自己编码.

Please see https://stackoverflow.com/a/23092644/2808624 below: Nowadays one would use SphericalUtil for moving LatLng coordinates, instead of coding it on your own.

这篇关于在 LatLngBounds 构建器上设置最大缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆