如何在Google Maps Android上“增加” VisibleRegion的大小

时间:2019-05-10 12:24:14

标签: android google-maps kotlin latitude-longitude

我正在尝试设置位于屏幕可见区域内的可见标记。至此,我可以实现:

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

但是当用户进行一些滚动操作时,我需要将标记从该可见区域稍微“远”加载到“防止”位置。我们假设如果用户滚动到很远的地方,标记“将稍后出现”。

所以我的问题是如何计算这个“额外”空间。我不知道是否向latlan西南/东北点添加一些小数,或者我需要一些特定的数学运算

enter image description here

1 个答案:

答案 0 :(得分:0)

要“增加” 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
    }
}