【问题标题】:How to "increase" size of VisibleRegion on Google Maps Android如何在 Google Maps Android 上“增加” VisibleRegion 的大小
【发布时间】:2019-09-28 08:48:30
【问题描述】:

我正在尝试在屏幕可见区域内设置可见标记。在这一点上我能够实现:

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 西南/东北点添加一些小数,或者我需要一些特定的数学

【问题讨论】:

  • 它也应该取决于缩放级别。但这听起来不像是一项需要更多数学运算的任务,而是将界限扩大一个合理的线性量。

标签: android google-maps kotlin latitude-longitude


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-01-04
    • 2013-03-19
    • 2015-10-21
    • 1970-01-01
    • 2016-06-13
    • 2014-10-15
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    相关资源
    最近更新 更多