根据经纬度查询附近的点
来源:互联网 发布:防御sql注入原理 编辑:程序博客网 时间:2024/04/30 14:32
根据给定经纬度(lat,lng)求出其左上角(left_top),右上角(right_top),左下角(left_bottom),右下角(right_bottom)的四个位置。所有在这个区域的范围都在该点附近。
package com.show.common.util;
public class Location {
private double latitude;
private double longitude;
public Location(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
package com.show.common.util;
public class LatitudeLontitudeUtil {
// http://blog.charlee.li/location-search/
/** 地球半径 */
private static final double EARTH_RADIUS = 6371000;
/** 范围距离 */
private double distance;
/** 左上角 */
private Location left_top = null;
/** 右上角 */
private Location right_top = null;
/** 左下角 */
private Location left_bottom = null;
/** 右下角 */
private Location right_bottom = null;
private LatitudeLontitudeUtil(double distance) {
this.distance = distance;
}
private void getRectangle4Point(double lat, double lng) {
// float dlng = 2 * asin(sin(distance / (2 * EARTH_RADIUS)) / cos(lat));
// float dlng = degrees(dlng) // 弧度转换成角度
double dlng = 2 * Math.asin(Math.sin(distance / (2 * EARTH_RADIUS))
/ Math.cos(lat));
dlng = Math.toDegrees(dlng);
// dlat = distance / EARTH_RADIUS
// dlng = degrees(dlat) # 弧度转换成角度
double dlat = distance / EARTH_RADIUS;
dlat = Math.toDegrees(dlat); // # 弧度转换成角度
// left-top : (lat + dlat, lng - dlng)
// right-top : (lat + dlat, lng + dlng)
// left-bottom : (lat - dlat, lng - dlng)
// right-bottom: (lat - dlat, lng + dlng)
left_top = new Location(lat + dlat, lng - dlng);
right_top = new Location(lat + dlat, lng + dlng);
left_bottom = new Location(lat - dlat, lng - dlng);
right_bottom = new Location(lat - dlat, lng + dlng);
}
public static double hav(double theta) {
double s = Math.sin(theta / 2);
return s * s;
}
public static double getDistance(double lat0, double lng0, double lat1,
double lng1) {
// from math import sin, asin, cos, radians, fabs, sqrt
// def hav(theta):
// s = sin(theta / 2)
// return s * s
// def get_distance_hav(lat0, lng0, lat1, lng1):
// "用haversine公式计算球面两点间的距离。"
// # 经纬度转换成弧度
// lat0 = radians(lat0)
// lat1 = radians(lat1)
// lng0 = radians(lng0)
// lng1 = radians(lng1)
// dlng = fabs(lng0 - lng1)
// dlat = fabs(lat0 - lat1)
// h = hav(dlat) + cos(lat0) * cos(lat1) * hav(dlng)
// distance = 2 * EARTH_RADIUS * asin(sqrt(h))
// return distance
lat0 = Math.toRadians(lat0);
lat1 = Math.toRadians(lat1);
lng0 = Math.toRadians(lng0);
lng1 = Math.toRadians(lng1);
double dlng = Math.abs(lng0 - lng1);
double dlat = Math.abs(lat0 - lat1);
double h = hav(dlat) + Math.cos(lat0) * Math.cos(lat1) * hav(dlng);
double distance = 2 * EARTH_RADIUS * Math.asin(Math.sqrt(h));
return distance;
}
public static Location[] getRectangle4Point(double lat, double lng,
double distance) {
LatitudeLontitudeUtil llu = new LatitudeLontitudeUtil(distance);
llu.getRectangle4Point(lat, lng);
Location[] locations = new Location[4];
locations[0] = llu.left_top;
locations[1] = llu.right_top;
locations[2] = llu.left_bottom;
locations[3] = llu.right_bottom;
return locations;
}
public static void main(String[] args) {
double lat = 30.500;
double lng = 120.500;
double distance = 500d;
Location[] locations = LatitudeLontitudeUtil.getRectangle4Point(lat,
lng, distance);
String sql = "SELECT * FROM place WHERE lat > "
+ locations[2].getLatitude() + " AND lat < "
+ locations[0].getLatitude() + " AND lng > "
+ locations[0].getLongitude() + " AND lng < "
+ locations[1].getLongitude();
System.out.println(sql);
double lat1 = 30.495503391970406;
double lng1 = 120.49261708577215;
double d = LatitudeLontitudeUtil.getDistance(lat, lng, lat1, lng1);
System.out.println(d);
}
}
- 根据经纬度查询附近的点
- 根据经纬度查询附近的点
- 根据一个给定经纬度的点,进行附近地点查询
- 根据一个给定经纬度的点,进行附近地点查询
- 在SAE上开发LBS应用之 根据一点的经纬度实现附近点的查询
- 在SAE上开发LBS应用之 根据一点的经纬度实现附近点的查询
- 开发LBS应用之 根据一点的经纬度实现附近点的查询 - geohash
- PHP,Mysql-根据一个给定经纬度的点,进行附近的人查询
- PHP,Mysql-根据一个给定经纬度的点,进行附近地点查询–合理利用算法
- PHP,Mysql-根据一个给定经纬度的点,进行附近地点查询–合理利用算法,
- PHP,Mysql-根据一个给定经纬度的点,进行附近地点查询–合理利用算法
- PHP,Mysql-根据一个给定经纬度的点,进行附近地点查询
- PHP,Mysql-根据一个给定经纬度的点,进行附近地点查询
- 根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
- PHP,Mysql-根据一个给定经纬度的点,进行附近地点查询
- PHP,Mysql-根据一个给定经纬度的点,进行附近地点查询
- 根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
- 根据一个给定经纬度的点和距离,进行附近地点查询
- pid相关命令
- hive启动报错 java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7B
- SSDB 安装 和 特性介绍
- 态度
- Linux 中的零拷贝技术,第 2 部分
- 根据经纬度查询附近的点
- javaFX——不同界面间的数据传递
- 【leetcode 190 Reverse Bits 】
- 敢说自己学过单片机的就必须弄明白的几件事,咱出去侃大山也有料
- Linux系统中一些目录和文件的详细解释(参照RHEL5.4)
- android:edittext学习记录
- NSRunLoop的浅显概述和原理
- 【SGU 176】 Flow construction
- Leetcode19 Remove Nth Node From End of List