java 经纬度两点 距离或范围

public class LatLonUtil {
	
    private static final double PI = 3.14159265;    //老祖真理
    private static final double EARTH_RADIUS = 6378137;    //赤道半径
    private static final double RAD = Math.PI / 180.0;

    //@see http://snipperize.todayclose.com/snippet/php/SQL-Query-to-Find-All-Retailers-Within-a-Given-Radius-of-a-Latitude-and-Longitude--65095/ 
	//The circumference of the earth is 24,901 miles.
    //24,901/360 = 69.17 miles / degree  
    /**
     * @param raidus 单位米
     * return minLat,minLng,maxLat,maxLng
     */
    public static double[] getAround(double lat,double lon,int raidus){
		
		Double latitude = lat;
		Double longitude = lon;
		
		Double degree = (24901*1609)/360.0;
		double raidusMile = raidus;
		
		Double dpmLat = 1/degree;
		Double radiusLat = dpmLat*raidusMile;
		Double minLat = latitude - radiusLat;
		Double maxLat = latitude + radiusLat;
		
		Double mpdLng = degree*Math.cos(latitude * (PI/180));
		Double dpmLng = 1 / mpdLng;
		Double radiusLng = dpmLng*raidusMile;
		Double minLng = longitude - radiusLng;
		Double maxLng = longitude + radiusLng;
		//System.out.println("["+minLat+","+minLng+","+maxLat+","+maxLng+"]");
		return new double[]{minLat,minLng,maxLat,maxLng};
	}
    
    /**
     * 根据两点间经纬度坐标(double值),计算两点间距离,单位为米
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     * @return
     */
    public static double getDistance(double lng1, double lat1, double lng2, double lat2)
    {
       double radLat1 = lat1*RAD;
       double radLat2 = lat2*RAD;
       double a = radLat1 - radLat2;
       double b = (lng1 - lng2)*RAD;
       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 = 34.264648;
		Double lon1 = 108.952736;
		
		int radius = 1000;
		//[34.25566276027792,108.94186385411045,34.27363323972208,108.96360814588955]
		getAround(lat1,lon1,radius);
		
		//911717.0   34.264648,108.952736,39.904549,116.407288
		double dis = getDistance(108.952736,34.264648,116.407288,39.904549); 
		System.out.println(dis);
	}
}

以上,最大经度,纬度,最小经度,纬度! 两点之间的距离 
以上引用别人,写的挺不错,但是有些麻烦!过去结果不够直接. 

oc中

    //object-c,获取搜索目标的经纬度
        var geocoder = new GClientGeocoder();  
        geocoder.getLocations(address, function($){  
        var lalton = $.Placemark[0].Point.coordinates;  
        areaMap(lalton[1],lalton[0]);  
        //alert(lalton[1] + "," + lalton[0]);  
    });  

注意这里数据库是mysql 

    SELECT id, ( 3959 * acos( cos( radians(lat_t) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(lng_t) )  
    + sin( radians(lat_t) ) * sin( radians( lat ) ) ) ) AS distance  
    FROM Stores HAVING distance < 25  
    ORDER BY distance  
    LIMIT 0,20  
SELECT * from 
app_activity where 
(3959*acos(cos(radians(36.0971114))*cos(radians(latitude))*cos(radians(longitude)-radians(103.6130684))+sin(radians(36.0971114))*sin(radians(latitude)))) <1

  

1、3959为地球半径 
2、25为搜索半径 
3、3959和25都是以“英里”为单位如果需要改成“公里”来计算的,都需要乘以1.60931
 

 

select * from location where sqrt(  
        (  
         ((113.914619-longitude)*PI()*12656*cos(((22.50128+latitude)/2)*PI()/180)/180)  
         *  
         ((113.914619-longitude)*PI()*12656*cos (((22.50128+latitude)/2)*PI()/180)/180)  
        )  
        +  
        (  
         ((22.50128-latitude)*PI()*12656/180)  
         *  
         ((22.50128-latitude)*PI()*12656/180)  
        ) 

  

经度:113.914619 
纬度:22.50128 
范围:2km 
longitude为数据表经度字段 
latitude为数据表纬度字段 


地球在线http://www.earthol.com/获取经纬度做测试

posted @ 2013-10-25 15:48  暖流  阅读(4124)  评论(0编辑  收藏  举报