Asp.net mvc计算两个地址之间的距离

时间:2014-11-23 03:04:54

标签: asp.net asp.net-mvc google-maps geolocation

我有一个asp.net mvc应用程序,用于存储用户信息(基本信息,地址信息等)。现在我需要的是,如果用户X搜索其他用户,则使用他们的地址信息显示所有其他用户Y以及X和Y之间的距离。用户的地址信息包含地址,城市,州,邮政编码和国家。我是地理位置,谷歌地图的新手。请建议我“如何计算asp.net mvc中两个地址之间的实际距离”。

2 个答案:

答案 0 :(得分: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 sales@geodatasource.com                  :::
//:::                                                                         :::
//:::  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"));

答案 1 :(得分:0)

首先,您需要使用Google Map 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