/// <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);
}
}
GSM基站定位信息解析为经纬度方法
最新推荐文章于 2022-09-08 23:19:35 发布