高德地图计算两点之间的距离java+html

高德地图现在使用比较多了,但是高德地图计算两点之间的距离有些问题。下面提供一个很好的解决方案。

代码如下:


  /**
     * 两点的经纬度获取两点距离米
     * @param (121.293986,37.279148,121.284545,37.292396)
     * @returns {string}
     * @author zhangyufeng
     */
    $rootScope.getGDDTDistance = function (startlongitude,startdimensionality,endlongitude,enddimensionality) {
     var   lon1 = (Math.PI / 180) * startlongitude;
     var  lon2 = (Math.PI / 180) * endlongitude;
     var   lat1 = (Math.PI / 180) * startdimensionality;
     var  lat2 = (Math.PI / 180) * enddimensionality;
        // 地球半径
     var   R = 6371;
        // 两点间距离 km,如果想要米的话,结果*1000就可以了
        var   d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;
        return d * 1000;

    };

/**
     * 获取距离
     * @param distance
     * @returns {string}
     * @author lixiang
     */
    $rootScope.getDistance = function (distance) {
    	if(distance==null){
    		return "";
    	}
        var meter = 'm';
        var kilometer = 'km';
        var result = '';
        if (distance < 1000) {
            result = distance.toFixed(0) + meter;
        } else {
            var km = distance / 1000;
            if (km >= 100) {
                //超过100公里 去掉小数点
                result = (distance / 1000).toFixed(0) + kilometer;
            } else if(km >= 10){
                //10-100公里以内 1位小数
                result = (distance / 1000).toFixed(1) + kilometer;
            } else {
                //10公里以内 2位小数
                result = (distance / 1000).toFixed(2) + kilometer;
            }
        }
        return result;
    };


 前台调用 {{getDistance(getGDDTDistance(121.293986,37.279148,121.284545,37.292396))}}
public class Distribution  
{  
    // 经度  
    double longitude;  
    // 维度  
    double dimensionality;  
  
    public double getLongitude()  
    {  
        return longitude;  
    }  
  
    public void setLongitude(double longitude)  
    {  
        this.longitude = longitude;  
    }  
  
    public double getDimensionality()  
    {  
        return dimensionality;  
    }  
  
    public void setDimensionality(double dimensionality)  
    {  
        this.dimensionality = dimensionality;  
    }  
  
    /* 
     * 计算两点之间距离 
     *  
     * @param start 
     *  
     * @param end 
     *  
     * @return 米 
     */  
    public double getDistance(Distribution start, Distribution end)  
    {  
  
        double lon1 = (Math.PI / 180) * start.longitude;  
        double lon2 = (Math.PI / 180) * end.longitude;  
        double lat1 = (Math.PI / 180) * start.dimensionality;  
        double lat2 = (Math.PI / 180) * end.dimensionality;  
  
        // double Lat1r = (Math.PI/180)*(gp1.getLatitudeE6()/1E6);  
        // double Lat2r = (Math.PI/180)*(gp2.getLatitudeE6()/1E6);  
        // double Lon1r = (Math.PI/180)*(gp1.getLongitudeE6()/1E6);  
        // double Lon2r = (Math.PI/180)*(gp2.getLongitudeE6()/1E6);  
  
        // 地球半径  
        double R = 6371;  
  
        // 两点间距离 km,如果想要米的话,结果*1000就可以了  
        double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;  
  
        return d * 1000;  
    }  
}  




    
    
    
    两点间距离
    
    
    



  view 
 
  
 
  
 
  
 
 

你可能感兴趣的:(高德地图)