2014-11-23 84 views
0

我有一個存儲用戶信息(基本信息,地址信息等)的asp.net mvc應用程序。現在我需要的是,如果用戶X搜索其他用戶,然後使用他們的地址信息顯示所有其他用戶Y以及X和Y之間的距離。用戶的地址信息包含地址,城市,州,郵政編碼和國家。我對地理位置很陌生,谷歌地圖。請建議我「如何計算asp.net mvc中兩個地址之間的實際距離」。Asp.net mvc計算兩個地址之間的距離

回答

2

來自http://www.geodatasource.com/developers/c-sharp

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
//:::                   ::: 
//::: This routine calculates the distance between two points (given the  ::: 
//::: latitude/longitude of those points). It is being used to calculate  ::: 
//::: the distance between two locations using GeoDataSource(TM) products ::: 
//:::                   ::: 
//::: Definitions:               ::: 
//::: South latitudes are negative, east longitudes are positive   ::: 
//:::                   ::: 
//::: Passed to function:             ::: 
//::: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) ::: 
//::: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) ::: 
//::: unit = the unit you desire for results        ::: 
//:::   where: 'M' is statute miles         ::: 
//:::     'K' is kilometers (default)       ::: 
//:::     'N' is nautical miles         ::: 
//:::                   ::: 
//::: Worldwide cities and other features databases with latitude longitude ::: 
//::: are available at http://www.geodatasource.com       ::: 
//:::                   ::: 
//::: For enquiries, please contact [email protected]     ::: 
//:::                   ::: 
//::: Official Web site: http://www.geodatasource.com      ::: 
//:::                   ::: 
//:::   GeoDataSource.com (C) All Rights Reserved 2014    ::: 
//:::                   ::: 
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
private double distance(double lat1, double lon1, double lat2, double lon2, char unit) { 
    double theta = lon1 - lon2; 
    double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta)); 
    dist = Math.Acos(dist); 
    dist = rad2deg(dist); 
    dist = dist * 60 * 1.1515; 

    if (unit == 'K') { 
     dist = dist * 1.609344; 
    } else if (unit == 'N') { 
     dist = dist * 0.8684; 
    } 

    return (dist); 

} 

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

//:: This function converts decimal degrees to radians    ::: 

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
private double deg2rad(double deg) { 

    return (deg * Math.PI/180.0); 

} 



//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

//:: This function converts radians to decimal degrees    ::: 

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
private double rad2deg(double rad) { 

    return (rad/Math.PI * 180.0); 
} 



Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "M")); 
Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "K")); 
Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "N")); 
0

首先,你需要與谷歌地圖API來獲取你的地址的客戶端的座標。 這是可以做到這樣的: http://dotnet.dzone.com/news/using-google-geocode-get-gps

然後你就可以得到2個座標之間的距離:

var pointA = DbGeography.FromText("POINT (-2.232121 53.477724)", 4326); 
var pointB = DbGeography.FromText("POINT (-2.231105 53.478121)", 4326); 

var distanceAB = pointA.Distance(pointB); //distanceAB = 80.6382796064941 metres 
+1

堆棧溢出的答案應該是獨立的,即使它們的鏈路斷開。您應該在答案中包含該鏈接的相關內容。 – 2015-03-08 03:41:37

相關問題