【地图功能开发系列:一】根据当前坐标点获取距离不超过N公里的门店

在此处输入标题

声明变量

//假设当前坐标
double lon1 = 113.336028;
double lat1 = 23.21745;
//距离m
 double distance = 1000 * 5;
private static  double EARTH_RADIUS = 6378137;//赤道半径(单位m)

主要方法[LantitudeLongitudeDist]

public static double LantitudeLongitudeDist(double lon1, double lat1, double lon2, double lat2)
    {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);

        double radLon1 = rad(lon1);
        double radLon2 = rad(lon2);

        if (radLat1 < 0)
            radLat1 = Math.PI / 2 + Math.Abs(radLat1);// south  
        if (radLat1 > 0)
            radLat1 = Math.PI / 2 - Math.Abs(radLat1);// north  
        if (radLon1 < 0)
            radLon1 = Math.PI * 2 - Math.Abs(radLon1);// west  
        if (radLat2 < 0)
            radLat2 = Math.PI / 2 + Math.Abs(radLat2);// south  
        if (radLat2 > 0)
            radLat2 = Math.PI / 2 - Math.Abs(radLat2);// north  
        if (radLon2 < 0)
            radLon2 = Math.PI * 2 - Math.Abs(radLon2);// west  
        double x1 = EARTH_RADIUS * Math.Cos(radLon1) * Math.Sin(radLat1);
        double y1 = EARTH_RADIUS * Math.Sin(radLon1) * Math.Sin(radLat1);
        double z1 = EARTH_RADIUS * Math.Cos(radLat1);

        double x2 = EARTH_RADIUS * Math.Cos(radLon2) * Math.Sin(radLat2);
        double y2 = EARTH_RADIUS * Math.Sin(radLon2) * Math.Sin(radLat2);
        double z2 = EARTH_RADIUS * Math.Cos(radLat2);

        double d = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
        //余弦定理求夹角  
        double theta = Math.Acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS));
        double dist = theta * EARTH_RADIUS;
        return dist;
    }

遍历判断,得到符合条件的门店

 //准备存储符合条件门店的泛型类
  List<ShopPlaceInfo> placelist = new List<ShopPlaceInfo>();
        //查询数据库的门店地址坐标
        DataTable PlaceDt = kis_web.DBHelper.GetTable("select shopName, shopadress, Lat as lat,Lng as lon from Shop where Lat is not null and   Lng is not null");
        foreach (DataRow item in PlaceDt.Rows)
        {
            double currLon = Convert.ToDouble(item["lon"]);
            double currLat = Convert.ToDouble(item["lat"]);
            double place = LantitudeLongitudeDist(lon1, lat1, currLon, currLat);
            //如果当前距离和该门店距离相差小于指定N公里
            if (place <= distance)
            {
                //放进符合条件门店的泛型类中
                placelist.Add(new ShopPlaceInfo()
                {
                    lat = currLat,
                    lon = currLon,
                    saleQty = 1,
                    saleAmount = 100,
                    distance = place,
                    shopName = item["shopName"].ToString()

                });
            }
        }
       //这样最终得到了符合条件的门店Json数据
       string reJson = JsonConvert.SerializeObject(placelist);

参考文章地址
该参考地址里面的文章是用Java写的,我这里使用的这个方法转成了C#的
这是一个小系列,还有后续

2019/9/18后续补充

   private static double rad(double d)
        {
            return d * Math.PI / 180.0;
        }
posted @ 2019-04-24 16:33  君宁天下  阅读(...)  评论(...编辑  收藏
野生程序员真的是太难了,一刻也不敢停止学习