Get locations within radius

半腔热情 提交于 2019-12-03 21:16:49

Try this sample code,

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