Example #1
0
        /// <summary>
        /// 谷歌硬件解析
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static LocData GetLatLngFromGoogleLBSWIFIRequestEntity(GoogleLBSWIFIRequestEntity entity)
        {
            LocData locReturn = null;

            try
            {
                HttpUtil httpUtil     = new HttpUtil();
                var      postData     = JsonHelper.ToJson(entity);
                string   Key          = ConfigurationManager.AppSettings["GoogleGeoKey"].ToString();
                string   url          = "https://www.googleapis.com/geolocation/v1/geolocate?key=" + Key;
                var      responseData = httpUtil.PostRequest(url, postData);
                log4net.LogManager.GetLogger(typeof(GoogleParse).FullName).Info(responseData);
                GoogleLoction loca = JsonHelper.JSONToObject <GoogleLoction>(responseData);
                if (loca != null && loca.location != null)
                {
                    locReturn          = new LocData();
                    locReturn.lat      = loca.location.lat;
                    locReturn.lng      = loca.location.lng;
                    locReturn.accuracy = loca.accuracy;
                }
            }
            catch (Exception ex)
            {
                log4net.LogManager.GetLogger(typeof(GoogleParse).FullName).Error("GetLatLngFromGoogleLBSWIFIRequestEntity 操作失败:" + ex.Message, ex);
            }
            return(locReturn);
        }
        /// <summary>
        /// lbs wifi混合转换
        /// </summary>
        /// <param name="imei"></param>
        /// <param name="LbsItem"></param>
        /// <param name="WifiItems"></param>
        /// <returns></returns>
        public LocData GaoDeWIFILBSAnalysisPosition(String imei, List <LBSDataItem> LbsItem, List <WiFiItem> WifiItems)
        {
            String url = "";

            try
            {
                string Key     = ConfigurationManager.AppSettings["AmapPosition"].ToString();
                var    smac    = "";
                var    mmac    = "";
                var    macs    = "";
                var    nearbts = "";
                var    bts     = "";
                WifiItems = WifiItems.OrderByDescending(d => d.SignalValue).ToList();

                for (int i = 0; i < WifiItems.Count; i++)
                {
                    if (WifiItems[i].SignalValue > 0)//信号为负值
                    {
                        WifiItems[i].SignalValue = 0 - WifiItems[i].SignalValue;
                    }
                    if (String.IsNullOrEmpty(smac))
                    {
                        smac = WifiItems[i].Mac;
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(mmac))
                        {
                            mmac = WifiItems[i].ToString();
                        }
                        macs += String.Format("{0},{1},{2}|", WifiItems[i].Mac, WifiItems[i].SignalValue, WifiItems[i].Name);
                    }
                }
                macs = macs.Trim('|');
                foreach (var item in LbsItem)
                {
                    if (item.signal > 0)//信号为负值
                    {
                        item.signal = 0 - item.signal;
                    }

                    if (String.IsNullOrEmpty(bts))
                    {
                        bts = String.Format("{0},{1},{2},{3},{4}", item.mcc, item.mnc, item.lac, item.cellid, item.signal);
                    }
                    else
                    {
                        nearbts += String.Format("{0},{1},{2},{3},{4}|", item.mcc, item.mnc, item.lac, item.cellid, item.signal);
                    }
                }
                nearbts = nearbts.Trim('|');

                url = String.Format("http://apilocate.amap.com/position?accesstype=1&imei={0}&smac={1}&mmac={2}&macs={3}&cdma=0&network=GSM&bts={4}&nearbts={5}&output=json&key={6}", imei, smac, mmac, macs, bts, nearbts, Key);
                HttpUtil httpu = new HttpUtil();
                JObject  obj   = JsonHelper.FromJson(httpu.GetRequest(url));

                if (obj != null && obj["status"] != null && obj["status"].ToString().Equals("1"))
                {
                    JObject result = JObject.Parse(obj["result"].ToString());
                    if (result != null && !result["type"].ToString().Equals("0"))
                    {
                        string  location = result["location"].ToString();
                        LocData lbsdat   = new LocData();
                        lbsdat.lng  = Convert.ToDouble(location.Split(',')[0]);
                        lbsdat.lat  = Convert.ToDouble(location.Split(',')[1]); //纬度
                        lbsdat.Type = int.Parse(result["type"].ToString());

                        double radius = 0;
                        if (result["radius"] != null)
                        {
                            radius = Convert.ToDouble(result["radius"].ToString());
                        }
                        lbsdat.accuracy = radius;

                        if (result["desc"] != null)
                        {
                            lbsdat.address = string.Format("{0}附近约{1}米", result["desc"].ToString().Replace(" ", ""), radius);
                        }
                        if (result["radius"] != null)
                        {
                            radius = Convert.ToDouble(result["radius"].ToString());
                        }
                        if (result["country"] != null)
                        {
                            lbsdat.country = result["country"].ToString();
                        }
                        if (result["province"] != null)
                        {
                            lbsdat.province = result["province"].ToString();
                        }
                        if (result["city"] != null)
                        {
                            lbsdat.city = result["city"].ToString();
                        }
                        if (result["road"] != null)
                        {
                            lbsdat.road = result["road"].ToString();
                        }
                        if (result["poi"] != null)
                        {
                            lbsdat.poi = result["poi"].ToString();
                        }
                        return(lbsdat);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    if (obj != null && obj["info"] != null)
                    {
                        log4net.LogManager.GetLogger("定位失败").Error("调用高德lbs数据解析地址错误 code:" + obj["info"].ToString() + "  url:" + url);
                    }

                    return(null);
                }
            }
            catch (Exception ex)
            {
                Type   classt   = this.GetType();
                string typeName = classt.ToString();                                                //空间名.类名
                string tname    = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name; //方法名
                string Title    = typeName + "." + tname;
                log4net.LogManager.GetLogger(Title).Error("调用高德Wifi数据解析地址错误:" + ex.Message + "  url:" + url, ex);
                return(null);
            }
        }
        /// <summary>
        /// 基站转换
        /// </summary>
        /// <param name="dataItem"></param>
        /// <returns></returns>
        public LocData LBSAnalysisPosition(String imei, List <LBSDataItem> dataItem)
        {
            String url = "";

            try
            {
                string IMEI    = imei;
                string SMAC    = "";     //手机MAC地址
                string MMAC    = "";     //已连热点 mac信
                string Bts     = "";     //接入 基站信息
                string NearBts = "";     //周边基站信息 (不 含接入 基站 信息 mcc,mnc,lac,cellid,signal)
                string OutPut  = "json"; //返回数据格式 xml json

                for (int i = 0; i < dataItem.Count; i++)
                {
                    if (dataItem[i].signal > 0)//信号为负值
                    {
                        dataItem[i].signal = 0 - dataItem[i].signal;
                    }

                    //默认第一位当前接入的基站
                    if (i == 0)
                    {
                        Bts = dataItem[i].mcc + "," + dataItem[i].mnc + "," + dataItem[i].lac + "," + dataItem[i].cellid + "," + dataItem[i].signal;
                    }
                    else
                    {
                        NearBts += dataItem[i].mcc + "," + dataItem[i].mnc + "," + dataItem[i].lac + "," + dataItem[i].cellid + "," + dataItem[i].signal + "|";
                    }
                }
                if (NearBts.Trim().Length > 0)
                {
                    NearBts = NearBts.Substring(0, NearBts.Length - 1);
                }


                string Key = ConfigurationManager.AppSettings["AmapPosition"].ToString();
                url = String.Format("http://apilocate.amap.com/position?accesstype=0&network=GSM&imei={0}&smac={1}&cdma=0&bts={2}&nearbts={3}&output={4}&key={5}", IMEI, SMAC, Bts, NearBts, OutPut, Key);
                HttpUtil httpu = new HttpUtil();
                JObject  obj   = JsonHelper.FromJson(httpu.GetRequest(url));

                if (obj != null && obj["status"] != null && obj["status"].ToString().Equals("1"))
                {
                    JObject result = JObject.Parse(obj["result"].ToString());
                    if (result != null && !result["type"].ToString().Equals("0"))
                    {
                        string  location = result["location"].ToString();
                        LocData lbsdat   = new LocData();
                        lbsdat.lng  = Convert.ToDouble(location.Split(',')[0]);
                        lbsdat.lat  = Convert.ToDouble(location.Split(',')[1]); //纬度
                        lbsdat.Type = int.Parse(result["type"].ToString());

                        double radius = 0;
                        if (result["radius"] != null)
                        {
                            radius = Convert.ToDouble(result["radius"].ToString());
                        }
                        lbsdat.accuracy = radius;

                        if (result["desc"] != null)
                        {
                            lbsdat.address = string.Format("{0}附近约{1}米", result["desc"].ToString().Replace(" ", ""), radius);
                        }
                        if (result["radius"] != null)
                        {
                            radius = Convert.ToDouble(result["radius"].ToString());
                        }
                        if (result["country"] != null)
                        {
                            lbsdat.country = result["country"].ToString();
                        }
                        if (result["province"] != null)
                        {
                            lbsdat.province = result["province"].ToString();
                        }
                        if (result["city"] != null)
                        {
                            lbsdat.city = result["city"].ToString();
                        }
                        if (result["road"] != null)
                        {
                            lbsdat.road = result["road"].ToString();
                        }
                        if (result["poi"] != null)
                        {
                            lbsdat.poi = result["poi"].ToString();
                        }
                        return(lbsdat);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    if (obj != null && obj["info"] != null)
                    {
                        log4net.LogManager.GetLogger("定位失败").Error("调用高德lbs数据解析地址错误 code:" + obj["info"].ToString() + "  url:" + url);
                    }

                    return(null);
                }
            }
            catch (Exception ex)
            {
                Type   classt   = this.GetType();
                string typeName = classt.ToString();                                                //空间名.类名
                string tname    = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name; //方法名
                string Title    = typeName + "." + tname;
                log4net.LogManager.GetLogger(Title).Error("调用高德LBS数据解析地址错误:" + ex.Message + "  url:" + url, ex);
                return(null);
            }
        }
        /// <summary>
        /// wifi 转换
        /// </summary>
        /// <param name="WiFiItems"></param>
        /// <returns></returns>
        public LocData WiFiAnalysisPosition(String imei, List <WiFiItem> WiFiItems)
        {
            String url = "";

            try
            {
                //List<WiFiItem> WiFiItems = new List<WiFiItem>();
                //WiFiItems.Add(new WiFiItem("22:27:1d:20:08:d5", -55, "CMCC-EDU"));
                //WiFiItems.Add(new WiFiItem("5c:63:bf:a4:bf:56", -86, "TP-LINK"));
                //WiFiItems.Add(new WiFiItem("d8:c7:c8:a8:1a:13", -42, "TP-LINK"));
                //--============

                if (WiFiItems == null || WiFiItems.Count <= 0)
                {
                    return(null);
                }

                string IMEI   = imei;
                string SMAC   = WiFiItems.First().Mac; //手机MAC地址
                string MMAC   = "";                    //已连热点 mac信
                string MACS   = "";                    //wifi列表中信息"f0:7d:68:9e:7d:18,-41,TPLink"
                string OutPut = "json";                //返回数据格式
                WiFiItems = WiFiItems.OrderByDescending(d => d.SignalValue).ToList();

                string Key = ConfigurationManager.AppSettings["AmapPosition"].ToString();


                for (int i = 0; i < WiFiItems.Count; i++)
                {
                    if (WiFiItems[i].SignalValue != 0)
                    {
                        if (WiFiItems[i].SignalValue > 0)    //信号为负值
                        {
                            WiFiItems[i].SignalValue = 0 - WiFiItems[i].SignalValue;
                        }
                        if (String.IsNullOrEmpty(MMAC))
                        {
                            MMAC = WiFiItems[i].ToString();
                        }
                        MACS += WiFiItems[i].ToString() + "|";
                    }
                }

                if (MACS.Length > 0)
                {
                    MACS = MACS.Substring(0, MACS.Length - 1);
                }
                url = String.Format("http://apilocate.amap.com/position?accesstype=1&imei={0}&network=GSM&cdma=0&smac={1}&mmac={2}&macs={3}&output={4}&key={5}", IMEI, SMAC, MMAC, MACS, OutPut, Key);
                HttpUtil httpu = new HttpUtil();
                JObject  obj   = JsonHelper.FromJson(httpu.GetRequest(url));

                if (obj != null && obj["status"] != null && obj["status"].ToString().Equals("1"))
                {
                    JObject result = JObject.Parse(obj["result"].ToString());
                    if (result != null && !result["type"].ToString().Equals("0"))
                    {
                        string  location = result["location"].ToString();
                        LocData lbsdat   = new LocData();
                        lbsdat.lng  = Convert.ToDouble(location.Split(',')[0]);
                        lbsdat.lat  = Convert.ToDouble(location.Split(',')[1]); //纬度
                        lbsdat.Type = int.Parse(result["type"].ToString());

                        double radius = 0;
                        if (result["radius"] != null)
                        {
                            radius = Convert.ToDouble(result["radius"].ToString());
                        }
                        lbsdat.accuracy = radius;

                        if (result["desc"] != null)
                        {
                            lbsdat.address = string.Format("{0}附近约{1}米", result["desc"].ToString().Replace(" ", ""), radius);
                        }
                        if (result["radius"] != null)
                        {
                            radius = Convert.ToDouble(result["radius"].ToString());
                        }
                        if (result["country"] != null)
                        {
                            lbsdat.country = result["country"].ToString();
                        }
                        if (result["province"] != null)
                        {
                            lbsdat.province = result["province"].ToString();
                        }
                        if (result["city"] != null)
                        {
                            lbsdat.city = result["city"].ToString();
                        }
                        if (result["road"] != null)
                        {
                            lbsdat.road = result["road"].ToString();
                        }
                        if (result["poi"] != null)
                        {
                            lbsdat.poi = result["poi"].ToString();
                        }
                        return(lbsdat);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    if (obj != null && obj["info"] != null)
                    {
                        log4net.LogManager.GetLogger("定位失败").Error("调用高德lbs数据解析地址错误 code:" + obj["info"].ToString() + "  url:" + url);
                    }

                    return(null);
                }
            }
            catch (Exception ex)
            {
                Type   classt   = this.GetType();
                string typeName = classt.ToString();                                                //空间名.类名
                string tname    = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name; //方法名
                string Title    = typeName + "." + tname;
                log4net.LogManager.GetLogger(Title).Error("调用高德Wifi数据解析地址错误:" + ex.Message + "  url:" + url, ex);
                return(null);
            }
        }