WCF REST 根据请求距离,返回附近用户的实例

来源:互联网 发布:.net网络爬虫 多线程 编辑:程序博客网 时间:2024/04/29 23:49

更新当前用户的坐标,并返回附近用户列表:

1.请求实体(UserLocationRequest):

public class UserLocationRequest    {        [DataMember(Name = "id")]        public Guid ID { get; set; }        //经度        [DataMember(Name = "longitude")]        public decimal Longitude { get; set; }        //纬度        [DataMember(Name = "latitude")]        public decimal Latitude { get; set; }        //距离/千米        [DataMember(Name = "distance")]        public decimal Distance{ get; set; }      }


2.IUserService:

        /// <summary>        /// 更新用户个人坐标,并根据距离单位返回附近用户        /// </summary>        /// <returns></returns>        [WebInvoke(UriTemplate = "/GetUserByDistance", Method = "POST",           RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]        IList<UserResponse> GetUserByDistance(UserLocationRequest request);

3.UserService:

public IList<UserResponse> GetUserByDistance(UserLocationRequest request)        {            try            {                User user = _db.Users.Single(m => m.ID == request.ID);                if (user != null)                {                    user.Longitude = request.Longitude;                    user.Latitude = request.Latitude;                    _db.SaveChanges();                    // GetUserByDistance 定义在数据库的存储过程,下面会附上(谨记对好参数顺序和逗号)                    const string sql = "exec GetUserByDistance @UserID,@Distance,@Longitude,@Latitude ";                    SqlParameter[] parameters = new[]                    {                        new SqlParameter("@UserID", request.ID),                        new SqlParameter("@Distance", request.Distance),                        new SqlParameter("@Longitude", request.Longitude),                        new SqlParameter("@Latitude", request.Latitude),                    };                    return _db.Database.SqlQuery<UserResponse>(sql, parameters).ToList();                }            }            catch (Exception)            {                return null;            }            return null;        }


4.存储过程(GetUserByDistance):
USE [XXX]GO/****** Object:  StoredProcedure [dbo].[GetUserByDistance]    Script Date: 12/21/2013 00:39:38 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author:<Author,,Name>-- Create date: <Create Date,,>-- Description:<Description,,>-- =============================================ALTER PROCEDURE [dbo].[GetUserByDistance]@UserID uniqueidentifier,@Distance decimal(18,4),@Longitude decimal(18,4),@Latitude decimal(18,4)ASBEGIN select *from (select ID,LoginName,Nickname,Intro,HeadImgUrl,dbo.fnGetDistance(Longitude,Latitude,@Longitude,@Latitude) as Distance     from Users where Latitude!=0 and Longitude!=0 and ID!=@UserID) T      where Distance<=@DistanceEND


5.用到计算两经纬度距离的函数(fnGetDistance):

USE [LingLiTong]GO/****** Object:  UserDefinedFunction [dbo].[fnGetDistance]    Script Date: 12/21/2013 22:13:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE FUNCTION [dbo].[fnGetDistance](@LatBegin REAL, @LngBegin REAL, @LatEnd REAL, @LngEnd REAL) RETURNS FLOAT AS BEGIN --距离(千米)  DECLARE @Distance REAL  DECLARE @EARTH_RADIUS REAL   SET @EARTH_RADIUS = 6378.137  DECLARE @RadLatBegin REAL,@RadLatEnd REAL,@RadLatDiff REAL,@RadLngDiff REAL  SET @RadLatBegin = @LatBegin *PI()/180.0  SET @RadLatEnd = @LatEnd *PI()/180.0  SET @RadLatDiff = @RadLatBegin - @RadLatEnd  SET @RadLngDiff = @LngBegin *PI()/180.0 - @LngEnd *PI()/180.0  SET @Distance = 2 *ASIN(SQRT(POWER(SIN(@RadLatDiff/2), 2)+COS(@RadLatBegin)*COS(@RadLatEnd)*POWER(SIN(@RadLngDiff/2), 2)))  SET @Distance = @Distance * @EARTH_RADIUS  --  SET @Distance = Round(@Distance * 10000) / 10000  RETURN @Distance ENDGO

6.User实体:

public  class User    {        public Guid ID { get; set; }        public String LoginName { get; set; } //登录名        public String Password { get; set; }  //密码         ....          public decimal Longitude { get; set; }   //经度        public decimal Latitude { get; set; }   //纬度    }

7.UserResponse

 public class UserResponse    {        [DataMember(Name = "id")]        public Guid ID { get; set; }        [DataMember(Name = "nickname")]        public String Nickname { get; set; }               [DataMember(Name = "distance")]        public double Distance { get; set; }      }




0 0
原创粉丝点击