经纬度距离搜索数据库操作以及hibernate操作、Java比较两点间的距离

news/2024/7/7 19:03:25

数据库语句:

select * from ls_region_user order by ACOS(SIN((39.975092 * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((39.975092 * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((116.385476 * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6378.140 asc;

select id,lat,lng, ACOS(SIN((39.975092 * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((39.975092 * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((116.385476 * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6378.140 from ls_region_user;

Hibernate语句:

String hql="from RegionUser u where u.lat is not null and u.lng is not null order by ACOS(SIN(("+lat.toString()+" * 3.1415) / 180 ) *SIN((u.lat * 3.1415) / 180 ) +COS(("+lat.toString()+" * 3.1415) / 180 ) * COS((u.lat * 3.1415) / 180 ) *COS(("+lng.toString()+" * 3.1415) / 180 - (u.lng * 3.1415) / 180 ) ) * 6378.140 asc";

Java方法:

public class LocationUtil {
	private static double EARTH_RADIUS = 6378.140;

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	/**
	 * 计算地球上任意两点(经纬度)距离
	 * 
	 * @param lat1
	 * @param lng1
	 * @param lat2
	 * @param lng2
	 * @return 千米
	 */
	public static double getDistance(double lat1, double lng1, double lat2,
			double lng2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		// s = Math.round(s * 10000) / 10000;
		return s;
	}

	public static void main(String[] args) {
		double lat1 = 39.975092;
		double lng1 = 116.385476;
		double lat2 = 39.985092;
		double lng2 = 116.395476;

		double aa = getDistance(lat1, lng1, lat2, lng2);
		System.out.println(StringUtil.doubleFormatByTwoA(aa*1000));

	}

}



http://www.niftyadmin.cn/n/1967379.html

相关文章

ajax传输

ajax:异步传输jsxml 异步刷新:网页中只有某一块需要修改,不影响其他块。比如说弹幕,点赞等! 实现: js: XMLHttpRequest 对象; 该对象的方法: open( 提交方式,服务器地址 ,true):与服务器建立连…

20170210解题报告

精英班考试题 2017.2.10 题目名 工资 藏妹子之处 银河之星 源文件 money.cpp/c/pas excel.pas/cpp galaxy.cpp/c/pas 输入文件 money.in excel.in galaxy.in 输出文件 money.out excel.out galaxy.out 时间限制 1000MS 1000MS 1000MS 内存限制 256MB 128…

MySQL 关于count与group by一起用的效果

本来想要效果是有多少个userId,结果却显示的是,去重后每组userId有多少个。 select count(id) from ls_wweb_log_call where userId is not null and companyUserId:companyUserId and createDate BETWEEN :nowDate AND :nextDate group by userId后来使…

Jackson将实体转为json形式,且未空或者null(不参加序列化)

Jackson将实体转为json形式,且未空或者null(不参加序列化), 常见用法: ObjectMapper mappernew ObjectMapper().setSerializationInclusion(JsonIclude.Inculde.NON_NULL) 常见还有:Include.ALAWAYS 默认;NON_DEFAULT 默认不序列化…

python的class里面的function怎么被调用

2019独角兽企业重金招聘Python工程师标准>>> 如果python class的类名和class的文件名相同,那么类中的function就默认是可见的。 例如: abc.py class abc { def function1() def function2 () } function1和function2都…

MySQL启动不起来和关闭不了的问题记录

昨天centos系统上的MySQL,不能启动(一直在启动中,不停的.....,也不报错,就一直启动中),也不能关闭(和启动效果一样)。 甚至: ps aux | grep mysql kill -9 1…

fetch传输方式

fetch传输方式 vue所利用的一种传输方式。 ①在前后台传输之前&#xff0c;需要配置好前台画面跳转的url; ②请求封装后&#xff0c;传输给后台url,返回请求数据。 ③将请求的到参数&#xff0c;导入对应的.vue,取值赋值。 <template><div class"detailsContai…

关于数据库工具_已迁移

为什么80%的码农都做不了架构师&#xff1f;>>> 数据库软件&#xff1a; Navicat、Power Designer 、Workbench 、 PHPmyAdmin 、MySQLDict(没有使用过、待试&#xff0c;如果你用过欢迎留言&#xff0c;留下你的博客地址。感谢^_^&#xff01;) 软件下载地址 powe…