【问题标题】:Only Plotting Markers Within a Drawn Circle or Perimeter Google Maps v2 Android仅在绘制的圆圈或周边内绘制标记 Google Maps v2 Android
【发布时间】:2013-02-26 16:37:59
【问题描述】:

基本上,标题说明了一切。我不想像现在那样在地图上绘制数据库中的每个条目,而是想查询数据库并只绘制坐标落在用户当前位置周围绘制的圆圈内的条目,但是我不能很想知道怎么做。目前,我已经编写了代码,在地图上绘制用户当前位置以及存储在我的数据库中的所有条目的位置以及当前位置周围的圆圈(见下图)。根据我下面的图片,我只想绘制圆圈内的三个标记。

有谁知道是否有任何方法可以检查存储在我的数据库中的纬度坐标是否在圆的区域内?或者如果没有,任何人都可以提出任何使用类似想法的替代方案。

我想到的另一种选择是使用正方形/矩形而不是圆形,这样我就可以简单地将条目的坐标与正方形/矩形的边界进行比较,但是我真的不知道这有多可行被视为 Google Maps API 不支持这些形状。我还遇到了LatLngBounds 类,它可能很有用,但我找不到任何使用它的示例代码。我该怎么做呢?

【问题讨论】:

    标签: android google-maps google-maps-markers geometry area


    【解决方案1】:

    我相信这个圆有一个固定的半径和一个中心点。

    所以,用这个方法来获取中心和一些LatLng之间的距离并设置一个条件

    距离

    public static String getDistance(LatLng ll_source, LatLng ll_destination,
            int unit) {
    
    
        int Radius = 6371;// radius of earth in Km
    
        double lat1 = ll_source.latitude;
        double lat2 = ll_destination.latitude;
        double lon1 = ll_source.longitude;
        double lon2 = ll_destination.longitude;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
                * Math.sin(dLon / 2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double valueResult = Radius * c;
        double km = valueResult / 1;
        DecimalFormat newFormat = new DecimalFormat("####");
        Integer kmInDec = Integer.valueOf(newFormat.format(km));
        double meter = valueResult % 1000;
        Integer meterInDec = Integer.valueOf(newFormat.format(meter));
        DecimalFormat df = new DecimalFormat("#.#");
        return df.format(valueResult);
    }
    

    【讨论】:

    • 谢谢,我自己找到了一些解决方案,但我肯定也会检查一下。
    【解决方案2】:

    好的,我已经找到了使用问题中提到的 LatLngBounds 类的解决方案。它的作用是:

    1. 围绕用户当前的纬度和经度坐标创建一个新的矩形周界(本示例大约为一平方公里)。
    2. 然后它会检查周界是否包含存储在数据库中的坐标。
    3. 如果是,则标记会绘制在地图上。

      public void getNearbyMarkers(){
          LatLngBounds perimeter = new LatLngBounds(new LatLng(currentLat - 0.004,
                  currentLon - 0.004), new LatLng(currentLat + 0.004, currentLon + 0.004));
      
      
          if (perimeter.contains(LatlongFromDatabase)) {
              //Plot Marker on Map
          } else {
              Toast.makeText(getApplicationContext(), "Co-ordinates not in perimeter!", 8).show();
          }
      }
      

    【讨论】:

      猜你喜欢
      • 2013-12-21
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      相关资源
      最近更新 更多