2014-11-23 110 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

相关问题