如何 "increase" Google 地图上的 VisibleRegion 大小 Android

How to "increase" size of VisibleRegion on Google Maps Android

我正在尝试在屏幕可见区域内设置可见标记。此时我能够实现:

this.googleMap.setOnCameraIdleListener {
    val bounds = this.googleMap.projection.visibleRegion.latLngBounds
    for (marker in this.markersUpForList) {
        if (bounds.contains(marker.position)) {
          marker.isVisible = true
        //... do more stuff
        } else {
          marker.isVisible = false
        }
    }
}

但是当用户滚动时,我需要将标记从这个可见区域 "far away" 加载到 "prevent"。我们假设如果用户滚动到远处,标记 "will appear later".

所以我的问题是如何计算这个"extra"space。我不知道例如是否将一些小数添加到 latlan southwest/northeast 点或者我需要一些特定的数学

要“增加”bounds 的大小,您可以使用 Affine Transformation(比例),例如就像在这个方法中:

private static LatLngBounds scaleBounds(LatLngBounds bounds, float scale, Projection projection) {
    LatLng center = bounds.getCenter();
    Point centerPoint = projection.toScreenLocation(center);

    Point screenPositionNortheast = projection.toScreenLocation(bounds.northeast);
    screenPositionNortheast.x = (int) (scale * (screenPositionNortheast.x - centerPoint.x) + centerPoint.x);
    screenPositionNortheast.y = (int) (scale * (screenPositionNortheast.y - centerPoint.y) + centerPoint.y);
    LatLng scaledNortheast = projection.fromScreenLocation(screenPositionNortheast);

    Point screenPositionSouthwest = projection.toScreenLocation(bounds.southwest);
    screenPositionSouthwest.x = (int) (scale * (screenPositionSouthwest.x - centerPoint.x) + centerPoint.x);
    screenPositionSouthwest.y = (int) (scale * (screenPositionSouthwest.y - centerPoint.y) + centerPoint.y);
    LatLng scaledSouthwest = projection.fromScreenLocation(screenPositionSouthwest);

    LatLngBounds scaledBounds = new LatLngBounds(scaledSouthwest, scaledNortheast);

    return scaledBounds;
}

你可以这样使用它:

...
val scaleFactor = 1.5f;  // increase size 1.5 times
val bounds = projection.getVisibleRegion().latLngBounds;
val scaledBounds = scaleBounds(bounds, scaleFactor, projection);

for (marker in this.markersUpForList) {
    if (scaledBounds .contains(marker.position)) {
      marker.isVisible = true
    //... do more stuff
    } else {
      marker.isVisible = false
    }
}