获取半径范围内的位置 [英] Get locations within radius

查看:118
本文介绍了获取半径范围内的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我已存储大约500条记录,每一个纬度和经度的数据库。

In a database I have stored around 500 records, each with a latitude and a longitude.

在一个活动我实施 LocationListener的 onLocationChanged -method我需要证明的是记录第XX米的位置半径内从该监听器接收。

In an Activity I implement a LocationListener and in the onLocationChanged-method I need to show the records that is within a radius of XX metres from the location received from this listener.

有一个(简单)的方式来做到这一点,无论是在SQL或Java?

Is there an (easy) way to do this, either in SQL or Java?

推荐答案

试试这个示例code,

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 */ 
                }

这篇关于获取半径范围内的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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