【问题标题】:Asp.net mvc calculate distance between two addressesAsp.net mvc 计算两个地址之间的距离
【发布时间】:2015-01-21 00:06:05
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    取自: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"));
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • Stack Overflow 的答案应该是独立的,即使它们的链接已损坏。您应该在答案中包含该链接中的相关内容。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      • 2011-12-24
      相关资源
      最近更新 更多