【问题标题】:How to calculate the bounding box around the touched map position?如何计算触摸地图位置周围的边界框?
【发布时间】:2015-02-11 16:21:20
【问题描述】:

在我的 Android 应用程序中,我想请求用户触摸地图的位置的数据。 GoogleMap.OnMapClickListener 提供触摸位置作为经纬度坐标。

public abstract void onMapClick(LatLng point)

为了传递一个区域而不是一个点,我需要计算一个以触摸位置为中心的边界框的坐标。边界框的扩展不应取决于地图的缩放级别。

我确实想要请求可见屏幕的边界框 - 只是触摸位置周围的一个小边界框区域。

我可以使用任何框架方法吗?否则,如何为边界框的扩展找到合适的距离值

【问题讨论】:

    标签: android google-maps-android-api-2 android-maps-v2 bounding-box


    【解决方案1】:

    LatLngBounds不是你需要的吗?

    【讨论】:

      【解决方案2】:

      你可以像下面这样覆盖onMarkerClick

      @Override
      public boolean onMarkerClick(Marker marker) {
      
          if (markerClicked) {
      
              if (polygon != null) {
                  polygon.remove();
                  polygon = null;
              }
      
              polygonOptions.add(marker.getPosition());
      
              polygonOptions.strokeColor(ContextCompat.getColor(getContext(), R.color.colorRedTransparent));
              polygonOptions.fillColor(ContextCompat.getColor(getContext(), R.color.colorBlueTransparent));
      
              polygonOptions.strokeWidth(5.0f);
              polygon = mMap.addPolygon(polygonOptions);
              polygon.setClickable(true);
      
          } else {
              if (polygon != null) {
                  polygon.remove();
                  polygon = null;
              }
      
              polygonOptions = new PolygonOptions().add(marker.getPosition());
              markerClicked = true;
          }
      
          return true;
      }
      

      传递你的多边形来生成边界框

      public static Rectangle getBoundingBox(Polygon polygon) {
      
          double boundsMinX = Double.MAX_VALUE; // bottom south latitude of the bounding box.
          double boundsMaxX = Double.MIN_VALUE; // top north latitude of bounding box.
      
          double boundsMinY = Double.MAX_VALUE; // left longitude of bounding box (western bound).
          double boundsMaxY = Double.MIN_VALUE; // right longitude of bounding box (eastern bound).
      
          for (int i = 0; i < polygon.getPoints().size(); i++) {
              double x = polygon.getPoints().get(i).latitude;
              boundsMinX = Math.min(boundsMinX, x);
              boundsMaxX = Math.max(boundsMaxX, x);
      
              double y = polygon.getPoints().get(i).longitude;
              boundsMinY = Math.min(boundsMinY, y);
              boundsMaxY = Math.max(boundsMaxY, y);
          }
           //Rectangle(double left, double bottom, double right, double top)
           return new Rectangle(boundsMinY, boundsMinX, boundsMaxY, boundsMaxX);
      }
      

      【讨论】:

        【解决方案3】:

        我不确定我是否完全理解您的问题,但通常屏幕到地图的坐标是使用几种方法完成的,可能是这样的:

        Projection projection = googleMaps.getProjection();

        Point p = projection.toScreenLocation(point);
        LatLng topLeft = projection.fromScreenLocation(new Point(p.x - halfWidth, p.y - halfHeight));
        LatLng bottomRight = projection.fromScreenLocation(new Point(p.x + halfWidth, p.y + halfHeight));
        

        编辑:

        以上

        • point 是您在onMapClick 上收到的LatLng
        • p 是该点击事件在屏幕上的位置
        • p.x - halfWidthp.y - halfHeightp.x + halfWidthp.y + halfHeight 是点击位置周围的边界框,宽度为 2 * halfWidth,高度为 2 * halfHeight
        • topLeftbottomRight 是在纬度-经度坐标中单击事件周围的边界框。

        【讨论】:

        • 如果我理解正确,您指的是可见屏幕的尺寸。那不是我想要的。我想计算触摸位置周围的边界框。
        • 边界框是像素还是米?
        • 但是halfWidth 的值是多少?这就是我面临的问题。
        • 这是你希望你的盒子的像素数。
        • @Budius 我不确定我想用像素表示框的尺寸。我宁愿用“地面单位”(例如米或厘米)来表示它们,这很难做到,因为坐标来自 WGS84。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-20
        • 2017-02-22
        • 2017-10-30
        • 1970-01-01
        • 2013-07-12
        • 2010-09-19
        • 2010-09-22
        相关资源
        最近更新 更多