java 读取sqlite_JavaSQlite获取最近的位置(经度和纬度)

小编典典

1)首先,以近似值过滤SQLite数据,并减少需要在Java代码中评估的数据量。为此,请使用以下过程:

为了具有确定性的阈值并更准确地过滤数据,最好在Java代码中计算以中心点的北,西,东和南为单位的4个位置,然后轻松检查小于和等于SQL运算符(>,

该方法calculateDerivedPosition(...)为你计算这些点(图片中的p1,p2,p3,p4)。

在此处输入图片说明

/**

* Calculates the end-point from a given source at a given range (meters)

* and bearing (degrees). This methods uses simple geometry equations to

* calculate the end-point.

*

* @param point

* Point of origin

* @param range

* Range in meters

* @param bearing

* Bearing in degrees

* @return End-point from the source given the desired range and bearing.

*/

public static PointF calculateDerivedPosition(PointF point,

double range, double bearing)

{

double EarthRadius = 6371000; // m

double latA = Math.toRadians(point.x);

double lonA = Math.toRadians(point.y);

double angularDistance = range / EarthRadius;

double trueCourse = Math.toRadians(bearing);

double lat = Math.asin(

Math.sin(latA) * Math.cos(angularDistance) +

Math.cos(latA) * Math.sin(angularDistance)

* Math.cos(trueCourse));

double dlon = Math.atan2(

Math.sin(trueCourse) * Math.sin(angularDistance)

* Math.cos(latA),

Math.cos(angularDistance) - Math.sin(latA) * Math.sin(lat));

double lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;

lat = Math.toDegrees(lat);

lon = Math.toDegrees(lon);

PointF newPoint = new PointF((float) lat, (float) lon);

return newPoint;

}

现在创建你的查询:

PointF center = new PointF(x, y);

final double mult = 1; // mult = 1.1; is more reliable

PointF p1 = calculateDerivedPosition(center, mult * radius, 0);

PointF p2 = calculateDerivedPosition(center, mult * radius, 90);

PointF p3 = calculateDerivedPosition(center, mult * radius, 180);

PointF p4 = calculateDerivedPosition(center, mult * radius, 270);

strWhere = " WHERE "

+ COL_X + " > " + String.valueOf(p3.x) + " AND "

+ COL_X + " < " + String.valueOf(p1.x) + " AND "

+ COL_Y + " < " + String.valueOf(p2.y) + " AND "

+ COL_Y + " > " + String.valueOf(p4.y);

+

COL_X是数据库中存储纬度值且COL_Y用于经度的列的名称。

因此,你可以得到一些近似于中心点的数据。

2)现在,你可以循环使用这些过滤后的数据,并使用以下方法确定它们是否真的在你的点附近(圆圈中):

public static boolean pointIsInCircle(PointF pointForCheck, PointF center,

double radius) {

if (getDistanceBetweenTwoPoints(pointForCheck, center) <= radius)

return true;

else

return false;

}

public static double getDistanceBetweenTwoPoints(PointF p1, PointF p2) {

double R = 6371000; // m

double dLat = Math.toRadians(p2.x - p1.x);

double dLon = Math.toRadians(p2.y - p1.y);

double lat1 = Math.toRadians(p1.x);

double lat2 = Math.toRadians(p2.x);

double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2)

* Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);

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

double d = R * c;

return d;

}

请享用!

我使用并定制了此参考资料并完成了它。

2020-02-29

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值