Android - 在地图中仅显示确定区域中包含的标记

时间:2016-06-09 22:49:45

标签: java android google-maps maps coordinates

我的应用程序带有地图。 在此地图中,我将标记放在设备的当前位置。我还在标记周围添加了一个圆圈,如下所示:

    Circle circle = mMap.addCircle(new CircleOptions()
                        .center(latLng)
                        .radius(400)     //The radius of the circle, specified in meters. It should be zero or greater.
                        .strokeColor(Color.rgb(0, 136, 255))
                        .fillColor(Color.argb(20, 0, 136, 255)));

的 结果是这样的:
here's an example of the result

我有一个数据库,其中一些位置以纬度和经度为特征。

我会在地图中设置标记,仅适用于位于之前添加的圆圈内的位置。
我如何理解该区域中包含哪些内容?

请帮助我,谢谢!

2 个答案:

答案 0 :(得分:4)

您可以添加所有标记,首先使它们不可见,然后计算圆心和标记之间的距离,使标记在给定距离内可见:

private List<Marker> markers = new ArrayList<>();

// ...

private void drawMap(LatLng latLng, List<LatLng> positions) {
    for (LatLng position : positions) {
        Marker marker = mMap.addMarker(
                new MarkerOptions()
                        .position(position)
                        .visible(false)); // Invisible for now
        markers.add(marker);
    }

    //Draw your circle
    Circle circle = mMap.addCircle(new CircleOptions()
            .center(latLng)
            .radius(400)
            .strokeColor(Color.rgb(0, 136, 255))
            .fillColor(Color.argb(20, 0, 136, 255)));

    for (Marker marker : markers) {
        if (SphericalUtil.computeDistanceBetween(latLng, marker.getPosition()) < 400) {
            marker.setVisible(true);
        }
    }
}

请注意,我使用了Google Maps API Utility Library

中的SphericalUtil.computeDistanceBetween方法

答案 1 :(得分:0)

您可以查看此问题,了解如何计算两个纬度经度之间的距离:how-to-calculate-distance-between-two-locations-using-their-longitude-and-latitu

2f2点之间的距离小于圆的半径(对于你400)为它们添加标记。(也不要只查看选择的答案。selected_location.distanceTo(another_location)会帮助你在下面回答。)

相关问题