根据坐标查询附近物品,根据距离并排序

方案一:sql编写

SELECT
	*,
	ROUND(
		6378.138 * 2 * ASIN(
			SQRT(
				POW( SIN( ( 30.317934 * PI( ) / 180- lat * PI( ) / 180 ) / 2 ), 2 ) + COS( 30.317934 * PI( ) / 180 ) * COS( lat * PI( ) / 180 ) * POW( SIN( ( 120.371382 * PI( ) / 180- lon * PI( ) / 180 ) / 2 ), 2 ) 
			) 
		) * 1000 
	) AS distance
FROM
	ancientpoetry 
HAVING
	distance <= 1000
ORDER BY
	distance ASC

方案二:代码编写

计算四个点的位置.

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 LatitudeLongitudeUtil {
    /**
     * 地球半径
     */

    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 LatitudeLongitudeUtil(double distance) {
        this.distance = distance;
    }

    private void getRectangle4Point(double lat, double lng) {
        double dlng = 2 * Math.asin(Math.sin(distance / (2 * EARTH_RADIUS)) / Math.cos(lat));
        dlng = Math.toDegrees(dlng);
        double dlat = distance / EARTH_RADIUS;
        dlat = Math.toDegrees(dlat); // # 弧度转换成角度
        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) {
// "用haversine公式计算球面两点间的距离。"
        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) {
        LatitudeLongitudeUtil llu = new LatitudeLongitudeUtil(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.317934;
        double lng = 120.371382;
        double distance = 500d;
        Location[] locations = LatitudeLongitudeUtil.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.317934;
        double lng1 = 120.371382;
        double d = LatitudeLongitudeUtil.getDistance(lat, lng, lat1, lng1);
        System.out.println(d);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值