Android Java google maps api v2:: how check with markers are within a circle on google maps android api v2

北城以北 提交于 2020-03-06 09:43:51

问题


How to check if markers are present within radius of circle and how to enable only those marker which are present under the area of circle?

I used circle option to create circle on map on click of marker. I just want only those markers visible which are inside the circle.

$$mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        CircleOptions circle = new CircleOptions();
        circle.center(latLng).fillColor(Color.LTGRAY).radius(1000);
        mMap.addCircle(circle);
        // circle.`enter code here`
        return true;
    }
});

回答1:


It should be pretty easy. Your circle has a latlng point and radius value. Calculate distance of each point from the circle center. Whose distance is less than the radius, they are in your circle and remaining are not. Here is the code to find distance between two latlngs.

private double distance(double lat1, double lon1, double lat2, double lon2) {
      double theta = lon1 - lon2;
      double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
      dist = Math.acos(dist);
      dist = rad2deg(dist);
      dist = dist * 60 * 1.1515;
       return (dist);
    }

   private double deg2rad(double deg) {
      return (deg * Math.PI / 180.0);
    }
   private double rad2deg(double rad) {
      return (rad * 180.0 / Math.PI);
    }

You should have list of latlngs that you used earlier to add markers to map.



来源:https://stackoverflow.com/questions/32840709/android-java-google-maps-api-v2-how-check-with-markers-are-within-a-circle-on

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