根据经纬度查询附近的点

来源:互联网 发布:华中数控编程实例带图 编辑:程序博客网 时间:2024/04/30 15:01
     
大致思想:根据给定经纬度(lat,lng)求出其左上角(left_top),右上角(right_top),左下角(left_bottom),右下角(right_bottom)的四个位置。所有在这个区域的范围都在该点附近。
参照:http://blog.charlee.li/location-search/

java代码:
  1. package com.show.common.util; 
  2.  
  3. public class Location { 
  4.     private double latitude; 
  5.     private double longitude; 
  6.  
  7.     public Location(double latitude,double longitude) { 
  8.         this.latitude = latitude; 
  9.         this.longitude = longitude; 
  10.     } 
  11.  
  12.     public double getLatitude() { 
  13.         return latitude; 
  14.     } 
  15.  
  16.     public void setLatitude(double latitude) { 
  17.         this.latitude = latitude; 
  18.     } 
  19.  
  20.     public double getLongitude() { 
  21.         return longitude; 
  22.     } 
  23.  
  24.     public void setLongitude(double longitude) { 
  25.         this.longitude = longitude; 
  26.     } 
  27.  

  1. package com.show.common.util; 
  2.  
  3. public class LatitudeLontitudeUtil { 
  4.  
  5.     // http://blog.charlee.li/location-search/ 
  6.  
  7.     /** 地球半径 */ 
  8.     private staticfinal double EARTH_RADIUS =6371000
  9.     /** 范围距离 */ 
  10.     private double distance; 
  11.     /** 左上角 */ 
  12.     private Location left_top = null
  13.     /** 右上角 */ 
  14.     private Location right_top = null
  15.     /** 左下角 */ 
  16.     private Location left_bottom = null
  17.     /** 右下角 */ 
  18.     private Location right_bottom = null
  19.  
  20.     private LatitudeLontitudeUtil(double distance) { 
  21.         this.distance = distance; 
  22.     } 
  23.  
  24.     private void getRectangle4Point(double lat,double lng) { 
  25.  
  26.         // float dlng = 2 * asin(sin(distance / (2 * EARTH_RADIUS)) / cos(lat)); 
  27.         // float dlng = degrees(dlng) // 弧度转换成角度 
  28.         double dlng = 2 * Math.asin(Math.sin(distance / (2 * EARTH_RADIUS)) 
  29.                 / Math.cos(lat)); 
  30.         dlng = Math.toDegrees(dlng); 
  31.  
  32.         // dlat = distance / EARTH_RADIUS 
  33.         // dlng = degrees(dlat) # 弧度转换成角度 
  34.         double dlat = distance / EARTH_RADIUS; 
  35.         dlat = Math.toDegrees(dlat); // # 弧度转换成角度 
  36.  
  37.         // left-top : (lat + dlat, lng - dlng) 
  38.         // right-top : (lat + dlat, lng + dlng) 
  39.         // left-bottom : (lat - dlat, lng - dlng) 
  40.         // right-bottom: (lat - dlat, lng + dlng) 
  41.         left_top = new Location(lat + dlat, lng - dlng); 
  42.         right_top = new Location(lat + dlat, lng + dlng); 
  43.         left_bottom = new Location(lat - dlat, lng - dlng); 
  44.         right_bottom = new Location(lat - dlat, lng + dlng); 
  45.  
  46.     } 
  47.  
  48.     public staticdouble hav(double theta) { 
  49.         double s = Math.sin(theta / 2); 
  50.         return s * s; 
  51.     } 
  52.  
  53.     public staticdouble getDistance(double lat0,double lng0, double lat1, 
  54.             double lng1) { 
  55.         // from math import sin, asin, cos, radians, fabs, sqrt 
  56.  
  57.         // def hav(theta): 
  58.         // s = sin(theta / 2) 
  59.         // return s * s 
  60.  
  61.         // def get_distance_hav(lat0, lng0, lat1, lng1): 
  62.         // "用haversine公式计算球面两点间的距离。" 
  63.         // # 经纬度转换成弧度 
  64.         // lat0 = radians(lat0) 
  65.         // lat1 = radians(lat1) 
  66.         // lng0 = radians(lng0) 
  67.         // lng1 = radians(lng1) 
  68.  
  69.         // dlng = fabs(lng0 - lng1) 
  70.         // dlat = fabs(lat0 - lat1) 
  71.         // h = hav(dlat) + cos(lat0) * cos(lat1) * hav(dlng) 
  72.         // distance = 2 * EARTH_RADIUS * asin(sqrt(h)) 
  73.  
  74.         // return distance 
  75.         lat0 = Math.toRadians(lat0); 
  76.         lat1 = Math.toRadians(lat1); 
  77.         lng0 = Math.toRadians(lng0); 
  78.         lng1 = Math.toRadians(lng1); 
  79.  
  80.         double dlng = Math.abs(lng0 - lng1); 
  81.         double dlat = Math.abs(lat0 - lat1); 
  82.         double h = hav(dlat) + Math.cos(lat0) * Math.cos(lat1) * hav(dlng); 
  83.         double distance = 2 * EARTH_RADIUS * Math.asin(Math.sqrt(h)); 
  84.  
  85.         return distance; 
  86.     } 
  87.  
  88.     public static Location[] getRectangle4Point(double lat,double lng, 
  89.             double distance) { 
  90.         LatitudeLontitudeUtil llu = new LatitudeLontitudeUtil(distance); 
  91.         llu.getRectangle4Point(lat, lng); 
  92.         Location[] locations = new Location[4]; 
  93.         locations[0] = llu.left_top; 
  94.         locations[1] = llu.right_top; 
  95.         locations[2] = llu.left_bottom; 
  96.         locations[3] = llu.right_bottom; 
  97.         return locations; 
  98.     } 
  99.  
  100.     public staticvoid main(String[] args) { 
  101.         double lat = 30.500
  102.         double lng = 120.500
  103.         double distance = 500d; 
  104.         Location[] locations = LatitudeLontitudeUtil.getRectangle4Point(lat, 
  105.                 lng, distance); 
  106.         String sql = "SELECT * FROM place WHERE lat > " 
  107.                 + locations[2].getLatitude() +" AND lat < " 
  108.                 + locations[0].getLatitude() +" AND lng > " 
  109.                 + locations[0].getLongitude() +" AND lng < " 
  110.                 + locations[1].getLongitude(); 
  111.         System.out.println(sql); 
  112.  
  113.         double lat1 = 30.495503391970406
  114.         double lng1 = 120.49261708577215
  115.         double d = LatitudeLontitudeUtil.getDistance(lat, lng, lat1, lng1); 
  116.         System.out.println(d); 
  117.     } 

 

 

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

0 0
原创粉丝点击