GSM基站定位信息解析为经纬度方法

         /// <summary>
        /// 将基站定位信息转化为 经纬度信息
        /// </summary>
        /// <param name="mcc">  移动用户所属国家代号:中国为460 </param>
        /// <param name="mnc">移动网号码,中国联通CDMA系统的MNC为03,中国移动的为00。</param>
        /// <param name="cellid">代表一个移动基站</param>
        /// <param name="lac">地区区域码</param>
        /// <param name="Lat">纬度</param>
        /// <param name="Lng">经度</param>
        public static void GoogleLoc(string mcc, string mnc, uint cellid, uint lac, out double Lat, out double Lng)
        {

            string json = "";
            json += "{";
            json += "\"version\":\"1.1.0\"" + ",";
            json += "\"host\":\"maps.google.com\"" + ",";
            json += "\"request_address\": \"false\"" + ",";
            json += "\"address_language\": \"zh_CN\"" + ",";
            json += "\"radio_type\":\"gsm\"" + ",";
            json += "\"cell_towers\":[";

            json += "{";
            json += "\"cell_id\":" + cellid + ",";
            json += "\"location_area_code\":" + lac + ",";
            json += "\"mobile_country_code\":" + mcc + ",";
            json += "\"mobile_network_code\":" + mnc;
            json += "}";

            json += "]}";

            string requestJson = json;
            string responseJson = string.Empty;
            try
            {
                ServicePointManager.Expect100Continue = false;
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                byte[] data = encoding.GetBytes(requestJson);

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(@"http://www.google.com/loc/json");
                myRequest.Timeout = 16000;

                myRequest.Method = "POST";
                myRequest.ContentType = "application/requestJson";
                myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();

                newStream.Write(data, 0, data.Length);
                newStream.Close();

                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.Default);
                responseJson = reader.ReadToEnd();
                if (responseJson != "{}")
                {
                    try
                    {
                        int index1 = responseJson.IndexOf("latitude");
                        int index2 = responseJson.IndexOf(":", index1);
                        int index3 = responseJson.IndexOf(",", index2);
                        Lat = Convert.ToDouble(responseJson.Substring(index2 + 1, index3 - index2 - 1));

                        index1 = responseJson.IndexOf("longitude");
                        index2 = responseJson.IndexOf(":", index1);
                        index3 = responseJson.IndexOf(",", index2);
                        Lng = Convert.ToDouble(responseJson.Substring(index2 + 1, index3 - index2 - 1));

                    }
                    catch (Exception)
                    {
                        throw new FormatException("解析google返回的json出错,返回内容为:" + responseJson);
                    }
                }
                else
                {
                    Lat = 0;
                    Lng = 0;

                    //ExceptionLogWriter.Write("没有此基站的位置信息,提交内容为:" + json);
                }
            }
            catch (Exception ex)
            {
                throw new IOException("向Google获取基站位置信息失败。提交内容为:" + json, ex);
            }

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值