Android 获取半径范围内的位置

Android 获取半径范围内的位置,android,sql,location,Android,Sql,Location,在一个数据库中,我存储了大约500条记录,每条记录都有一个纬度和一个经度 在一个活动中,我实现了一个LocationListener,在onLocationChanged-方法中,我需要显示距离此侦听器接收到的位置XX米半径内的记录 有没有一种(简单的)方法可以在SQL或Java中实现这一点?试试下面的示例代码 rs = st.executeQuery("SELECT longititude,latitude FROM location"); while (rs

在一个数据库中,我存储了大约500条记录,每条记录都有一个纬度和一个经度

在一个活动中,我实现了一个
LocationListener
,在
onLocationChanged
-方法中,我需要显示距离此侦听器接收到的位置XX米半径内的记录

有没有一种(简单的)方法可以在SQL或Java中实现这一点?

试试下面的示例代码

rs = st.executeQuery("SELECT longititude,latitude FROM  location");
                while (rs.next()) {
                double venueLat =rs.getDouble("latitude");
                double venueLng = rs.getDouble("longititude");

                double latDistance = Math.toRadians(userLat - venueLat);
                double lngDistance = Math.toRadians(userLng - venueLng);
                double a = (Math.sin(latDistance / 2) * Math.sin(latDistance / 2)) +
                                (Math.cos(Math.toRadians(userLat))) *
                                (Math.cos(Math.toRadians(venueLat))) *
                                (Math.sin(lngDistance / 2)) *
                                (Math.sin(lngDistance / 2));

                double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

                double dist = 6371 * c;             
                if (dist<50){
                    /* Include your code here to display your records */ 
                }
rs=st.executeQuery(“从位置选择经纬度”);
while(rs.next()){
双venueLat=rs.getDouble(“纬度”);
双通道=rs.getDouble(“长通道”);
双latDistance=数学toRadians(userLat-venueLat);
双lngDistance=数学toRadians(userLng-venueLng);
双a=(Math.sin(latDistance/2)*Math.sin(latDistance/2))+
(Math.cos(Math.toRadians(userLat)))*
(Math.cos(Math.toRadians(venueLat)))*
(数学sin(距离/2))*
(数学sin(lngDistance/2));
double c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
双区=6371*c;

如果(distCheck),你能用简单的英语解释一下这个代码的作用吗?UserLat和UserLng是当前位置,对吗?distuserLat和UserLng是用户当前位置,venueLat和venueLng是数据库中的位置。dist是这两个位置之间的距离,单位为KM。6371是地球半径(平均半径=6371km)我可以参考或解释一下你使用的方程式吗?提前谢谢…@Coderji我知道我已经太迟了,但上面使用的方程式是。轻微的狡辩,但你可能可以计算数学。sin(latDistance/2)首先,然后在计算a时使用它,这将节省程序在每个周期计算4次,而Math.cos(Math.toRadians(userLat))可以在while循环之前,无需对每个记录都这样做。