Find closest location with longitude and latitude

前端 未结 6 1091
不知归路
不知归路 2020-12-12 17:20

I am working on a application where I need to get nearby location, my web service will receive 2 parameters (decimal longitude, decimal latitude )

I have a table wh

6条回答
  •  鱼传尺愫
    2020-12-12 18:08

    Here is Solution

    var constValue = 57.2957795130823D
    
    var constValue2 = 3958.75586574D;
    
    var searchWithin = 20;
    
    double latitude = ConversionHelper.SafeConvertToDoubleCultureInd(Latitude, 0),
                        longitude = ConversionHelper.SafeConvertToDoubleCultureInd(Longitude, 0);
    var loc = (from l in DB.locations
    let temp = Math.Sin(Convert.ToDouble(l.Latitude) / constValue) *  Math.Sin(Convert.ToDouble(latitude) / constValue) +
                                     Math.Cos(Convert.ToDouble(l.Latitude) / constValue) *
                                     Math.Cos(Convert.ToDouble(latitude) / constValue) *
                                     Math.Cos((Convert.ToDouble(longitude) / constValue) - (Convert.ToDouble(l.Longitude) / constValue))
                                 let calMiles = (constValue2 * Math.Acos(temp > 1 ? 1 : (temp < -1 ? -1 : temp)))
                                 where (l.Latitude > 0 && l.Longitude > 0)
                                 orderby calMiles
    
    select new location
      {
         Name = l.name
      });
      return loc .ToList();
    

提交回复
热议问题