Android-仅在地图中显示确定区域中包括的标记 [英] Android - display in the map only the markers included in a determinate area

查看:144
本文介绍了Android-仅在地图中显示确定区域中包括的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have my application with a map. In this map i put a marker in the current location of the device. I also add a circle around the marker as follow:

    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)));


结果是这样的:


这里是结果示例

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

I have a database with some positions characterized by a latitude and a longitude.

我只会在地图上设置标记,仅针对先前添加的圆内的位置。

我怎么知道该区域中包括哪些?

I would set markers in the map, only for positions that are located within the circle added previously.
How can I understand which of them are included in that area?

请帮助我,谢谢!

推荐答案

您可以添加所有标记,使它们最初不可见,然后计算圆心与标记之间的距离,可以看到给定距离内的标记:

You can add all your markers, making them invisible at first, and then compute the distance between the center of your circle and your markers, making visible the markers that are within a given distance:

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

请注意,我正在使用SphericalUtil.computeDistanceBetween 方法> Google Maps API实用程序库

Note that I'm using the SphericalUtil.computeDistanceBetween method from the Google Maps API Utility Library

这篇关于Android-仅在地图中显示确定区域中包括的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆