Android - display in the map only the markers included in a determinate area

隐身守侯 提交于 2019-12-10 16:14:46

问题


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

The result is something like this:
here's an example of the result!

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?

please help me, thanks!


回答1:


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

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




回答2:


You can take a look at this question for how to calculate distance between two latitude longitude : how-to-calculate-distance-between-two-locations-using-their-longitude-and-latitu

İf distance between 2 point is smaller than your Circle's radius(for you 400) put marker for them .(Also dont look only selected answer.There is selected_location.distanceTo(another_location) will help you in below answer. )



来源:https://stackoverflow.com/questions/37737426/android-display-in-the-map-only-the-markers-included-in-a-determinate-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!