根据经纬度查询附近的点

大致思想:根据给定经纬度(lat,lng)求出其左上角(left_top),右上角(right_top),左下角(left_bottom),右下角(right_bottom)的四个位置。所有在这个区域的范围都在该点附近。
参照:http://blog.charlee.li/location-search/

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);
    }
}

文章出自:http://0414.iteye.com/blog/2039199

根据经纬度查询附近的点

时间: 2024-08-28 22:22:15

根据经纬度查询附近的点的相关文章

根据经纬度查询附近企业 | SQL

--第一种 SELECT ent_id, ent_name, longitude, latitude, ROUND( 6378.138 * 2 * ASIN( SQRT( POW( SIN( ( 40.042307 * PI() / 180 - latitude * PI() / 180 ) / 2 ), 2 ) + COS(40.042307 * PI() / 180) * COS(latitude * PI() / 180) * POW( SIN( ( 116.317205 * PI() /

根据经纬度查询当前地点,百度google接口

<?php /** 百度 API   -----根据经纬度查询当前地点  **/      header("Content-Type: text/html; charset=UTF-8");    function getBaiduAPIAddress($lat,$lng){  //纬度,经度        $location = $lat.','.$lng;          $url = 'http://api.map.baidu.com/geocoder/v2/?locat

百度地图经纬度查询-ABCDEFGHIJKMHNOPQRSTUVWXYZ

百度地图经纬度查询-ABCDEFGHIJKMHNOPQRSTUVWXYZ: 搜索:<input type="text" size="20" name="keyword" id="keyword" /> <input type="submit" value="提交" onclick="MblogDotccMap(document.getElementById('

根据经纬度查询最近距离,mysql查询经纬度附近范围

public class Test{ private static List<LocalAddress> ilist = new ArrayList<LocalAddress>(); public static void main(String[] args) { Test test3 = new Test(); Double localDouble = 0.0; //定义一个二维数组存放经纬度 Double[][] doubles = { { 22.6036906766, 113

mysql经纬度查询并且计算2KM范围内附近用户的sql查询性能优化实例教程

之前很傻很天真地以为无非就是逐个计算距离,然后比较出来就行了,然后当碰到访问用户很多,而且数据库中经纬度信息很多的时候,计算量的迅速增长,能让服务器完全傻逼掉,还是老前辈的经验比我们丰富,给了我很大的启示. MySQL性能调优 – 使用更为快速的算法进行距离计算 最近遇到了一个问题,通过不断的尝试最终将某句原本占据近1秒的查询优化到了0.01秒,效率提高了100倍. 问题是这样的,有一张存放用户居住地点经纬度信息的MySQL数据表,表结构可以简化 为:id(int),longitude(long

GPS定位,根据经纬度查询附近地点的经纬度-sql方法实现

根据当前所在的坐标点也即经纬度,查找数据库中附近5公里或10公里附近的所有信息的实现,经过查找资料,原来是我高二学的,就是求弦长,数学忘完了,没想到数学还这么有用,数学啊 真是用途太大了. 用到的什么余弦,角度.弧度全忘完了,但是通过找资料,还是实现了,误差相对还是比较小的,我在mssql数据库中实现的,具体如下: mssql函数: ALTER FUNCTION [GetDistance] ( @GPSLng DECIMAL(12,6), @GPSLat DECIMAL(12,6), @Lng

Mysql 地区经纬度 查询

摘要: Mysql数据库,根据地区的经纬度信息,查询附近相邻的地区 2015-03-27 修改,增加 MySQL的空间扩展(MySQL Spatial Extensions)的解决方案: MySQL的空间扩展(MySQL Spatial Extensions),它允许在MySQL中直接处理.保存和分析地理位置相关的信息,看起来这是使用MySQL处理地理位置信息的“官方解决方案”. 但恰恰很可惜的是:它却不支持某些最基本的地理位置操作,比如查询在半径范围内的所有数据.它甚至连两坐标点之间的距离计算

百度地图的反地址解析(通过经纬度查询地址信息)

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <script type="text/javascript" src="./jquery-1.4.2.min.js"></script> <!-- 这里的引用有ak和没有ak的区别暂时还没有弄清楚

elasticsearch 经纬度查询

curl -XPUT "http://localhost:9200/shop/" -d '{   "mappings": {     "shop": {       "properties": {         "name": {           "type": "string"         },         "location":