通过Web页面获取基站位置(Web端,源码下载)转

本文涉及的产品
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
简介:

 

源代码地址:http://files.cnblogs.com/psunny/SafetyNetMobileHttpService.rar

我在使用Windows Mobile向http://www.google.com/loc/json请求基站信息时,发现第一次发送基站信息能够获取到一个经纬度,第二次却失败,直接导致异常退出应用程序。由于基站信息只能在真机上才能获取,无法进行调试,我就以一个中间web页面来向手机发送位置信息,再者拼接Json和解析的Json都放在web上进行处理,也减少Windows Mobile应用程序的负担,Windows Mobile只单纯的负责接收一个经纬度字符串。

1. 具体思路

 

 

首先要清楚一点的是,每一个基站都能够通过请求http://www.google.com/loc/json获取到一个经纬度。如果用户能够在短时间内获取到较多的基站信息,比如4个或5个,可以通过这几个基站的经纬度计算出比较准确的用户位置。

举个例子,比如我在WM上取到4个基站信息:

50554,9513,460,1

50325,9513,460,1

50584,9513,460,1

50041,9513,460,1

每一行的4个数值分别对应:CID, LAC, MCC, MNC

 说明:

CID——CellID,表示基站号 
LAC——Location Area Code,表示区域编号

MCC——Mobile Country Code,表示国家编号,中国是460

MNC——Mobile Network Code,表示移动网络编号,移动是0,联通是1

更详细的MCC, MNC信息可以查看:http://en.wikipedia.org/wiki/Mobile_Network_Code

然后向Google发送基站信息是通过JSON串以Post的形式发送到http://www.google.com/loc/json,然后它会返回一个包含经纬度信息的JSON串。

RequestJson范例

{
"version": "1.1.0",
"host": "maps.google.com",
"cell_towers": [
{
"cell_id": 42,
"location_area_code": 415,
"mobile_country_code": 310,
"mobile_network_code": 410,
"age": 0,
"signal_strength": -60,
"timing_advance": 5555
},
{
"cell_id": 88,
"location_area_code": 415,
"mobile_country_code": 310,
"mobile_network_code": 580,
"age": 0,
"signal_strength": -70,
"timing_advance": 7777
}
],
"wifi_towers": [
{
"mac_address": "01-23-45-67-89-ab",
"signal_strength": 8,
"age": 0
},
{
"mac_address": "01-23-45-67-89-ac",
"signal_strength": 4,
"age": 0
}
]
}

然后从google那边返回来的位置信息如下:

ResponseJson样例

{
"location": {
"latitude": 51.0,
"longitude": -0.1,
"altitude": 30.1,
"accuracy": 1200.4,
"altitude_accuracy": 10.6,
"address": {
"street_number": "100",
"street": "Amphibian Walkway",
"postal_code": "94043",
"city": "Mountain View",
"county": "Mountain View County",
"region": "California",
"country": "United States of America",
"country_code": "US"
}
},
"access_token": "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe"
}

 

 说明:

即使只发送一个基站信息,也能够获取到一个经纬度。以上的RequestJson和ResponseJson只是样例,在实际Windows Mobile运用中,有可能取不到timing_adavance和singal_strength这两个参数,结合这两个参数定位会更准确一些。在我的代码例子中,我只取了CID, LAC, MCC, MNC参数。

关于JSON串中的属性各代表什么意思,详细请看:http://code.google.com/intl/zh-CN/apis/gears/geolocation_network_protocol.html

 

从上面的两端代码中我们可以大致了解到如何向Google发送基站请求,获取经纬度,下面我就以简单的代码来实现获取经纬度的方法。其实了解了上面的代码后,我们主要的工作就是拼接RequestJson串和解析
ResponseJson串,解析Json串我使用的是JSON.NET

2.实现

 
 
基站类:CellInfo.cs
using System;

/// <summary>
/// CellInfocs 的摘要说明
/// </summary>
public class CellInfo
{
public string cid;
public string lac;
public string mcc;
public string mnc;

public string CID
{
get { return cid; }
set { cid = value; }
}

public string LAC
{
get { return lac; }
set { lac = value; }
}

public string MCC
{
get { return mcc; }
set { mcc = value; }
}

public string MNC
{
get { return mnc; }
set { mnc = value; }
}

}

 

主要的类:LocationService.cs 
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;

/// <summary>
/// LocationService 的摘要说明
/// </summary>
public class LocationService
{
public static string ErrorMessage;

public LocationService(string postData)
{
GetLocationInfomation(postData);
}

/// <summary>
/// 返回经纬度信息
/// 格式如下:
/// 22.506421,113.918245|22.497636,113.912647|22.496063,113.91121
/// </summary>
/// <param name="postData"></param>
/// <returns></returns>
public string GetLocationInfomation(string postData)
{
List<CellInfo> list = GetCellInfos(postData);
StringBuilder sb = new StringBuilder();

for (int i = 0; i < list.Count; i++)
{
CellInfo info = list[i];
//基本步骤
//1. 生成发往google的json串
//2. 接收google返回的json串
//3. 解析json串,只取得经纬度
//4. 拼接经纬度
string json = GenerateRequestJson(info);
string content = GetResponseJson(json);
string latLon = ParseResponseJson(content);

sb.Append(latLon);
sb.Append("|");
}

return sb.ToString().Substring(0, sb.Length - 1);
//return sb.ToString();
}

/// <summary>
/// 接收从手机端发送过来的数据
/// '|'分割对象,','分割属性
/// </summary>
/// <param name="postData"></param>
/// <returns></returns>
private List<CellInfo> GetCellInfos(string postData)
{
string[] strInfos = postData.Split('|');
List<CellInfo> list = new List<CellInfo>();
for (int i = 0; i < strInfos.Length; i++)
{
string[] properties = strInfos[i].Split(',');
CellInfo info = new CellInfo();

info.CID = properties[0];
info.LAC = properties[1];
info.MCC = properties[2];
info.MNC = properties[3];

list.Add(info);
}
return list;
}


/// <summary>
/// 发送一个基站信息,并返回一个位置信息
/// 位置信息是以json串的形式存在
/// 需要对json串进行解析
/// </summary>
/// <param name="requestJson"></param>
private string GetResponseJson(string requestJson)
{
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.Method = "POST";
myRequest.ContentType = "application/requestJson";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();

// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();

// Get response JSON string
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

StreamReader reader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.Default);
responseJson = reader.ReadToEnd();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}

return responseJson;
}

/// <summary>
/// 解析从google Response的JSON串,获取经纬度
/// </summary>
/// <param name="responseJson"></param>
/// <returns></returns>
private string ParseResponseJson(string responseJson)
{
StringBuilder latLon = new StringBuilder();
JObject obj = JObject.Parse(responseJson);

string lat = obj["location"]["latitude"].ToString();
string lon = obj["location"]["longitude"].ToString();

latLon.Append(lat);
latLon.Append(",");
latLon.Append(lon);

return latLon.ToString();
}

/// <summary>
/// 生成发往http://www.google.com/loc/json的json串
/// 仅仅只有一个基站
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
private string GenerateRequestJson(CellInfo info)
{

string json = "";
json += "{";
json += "\"version\":\"1.1.0\"" + ",";
json += "\"host\":\"maps.google.com\"" + ",";
json += "\"cell_towers\":[";

json += "{";
json += "\"cell_id\":" + info.CID + ",";
json += "\"location_area_code\":" + info.LAC + ",";
json += "\"mobile_country_code\":" + info.MCC + ",";
json += "\"mobile_network_code\":" + info.MNC;
json += "}";

json += "],";
json += "\"wifi_towers\": [{}]";
json += "}";

return json;
}
}

 

中间的web页面:Default.aspx(发送请求时,就请求这个页面)

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

using System;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
string postData = Request.Form["POST_DATA"];
ResponseLocation(postData);
}


/// <summary>
/// 向手机端返回位置信息
/// </summary>
/// <param name="postData"></param>
public void ResponseLocation(string postData)
{
LocationService service = new LocationService(postData);
string location = service.GetLocationInfomation(postData);
Response.Write(location);
}
}

 

测试页面:index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Index Page</title>
</head>
<body>
<form id="form1" method="post" action="Default.aspx">
<input name="POST_DATA"
value=" 50554,9513,460,1|50325,9513,460,1|50584,9513,460,1|50041,9513,460,1"/>
<input type="submit"/>
</form>
</body>
</html> 本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2012/05/22/2512745.html如需转载请自行联系原作者
相关文章
|
3月前
|
传感器 小程序 搜索推荐
(源码)java开发的一套(智慧校园系统源码、电子班牌、原生小程序开发)多端展示:web端、saas端、家长端、教师端
通过电子班牌设备和智慧校园数据平台的统一管理,在电子班牌上,班牌展示、学生上课刷卡考勤、考勤状况汇总展示,课表展示,考场管理,请假管理,成绩查询,考试优秀标兵展示、校园通知展示,班级文化各片展示等多种化展示。
75 0
(源码)java开发的一套(智慧校园系统源码、电子班牌、原生小程序开发)多端展示:web端、saas端、家长端、教师端
|
15天前
|
存储
在 Web 中判断页面是不是刷新
【9月更文挑战第10天】在Web开发中,判断页面是否刷新有多种方法:1) 监听`popstate`事件,检测用户是否通过历史记录访问页面;2) 记录并比较页面加载时间戳,若相差极小,则可能为刷新;3) 利用本地存储设置特定值,若该值不存在或不符合预期,则页面可能被刷新。然而,这些方法并非绝对准确。
|
1月前
|
开发框架 前端开发 Java
【前端学java】SpringBootWeb极速入门-实现一个简单的web页面01
【8月更文挑战第12天】SpringBootWeb极速入门-实现一个简单的web页面01
52 3
【前端学java】SpringBootWeb极速入门-实现一个简单的web页面01
|
22天前
|
负载均衡 网络协议 应用服务中间件
web群集--rocky9.2源码部署nginx1.24的详细过程
Nginx 是一款由 Igor Sysoev 开发的开源高性能 HTTP 服务器和反向代理服务器,自 2004 年发布以来,以其高效、稳定和灵活的特点迅速成为许多网站和应用的首选。本文详细介绍了 Nginx 的核心概念、工作原理及常见使用场景,涵盖高并发处理、反向代理、负载均衡、低内存占用等特点,并提供了安装配置教程,适合开发者参考学习。
html,web页面朗读文字,朗读中文,朗读英文
html,web页面朗读文字,朗读中文,朗读英文
|
27天前
|
数据处理 开发者 UED
FastAPI 的模板引擎简直太神奇啦!这就是构建动态 Web 页面的终极秘籍,快来一探究竟!
【8月更文挑战第31天】FastAPI 是一款高性能异步 Web 框架,可通过集成模板引擎(如 Jinja2 或 Mako)实现动态页面渲染。使用模板引擎可分离页面结构与数据,简化代码并提升可维护性。此外,它还提供丰富的语法支持,如循环和条件判断,从而增强页面展示效果及开发效率。通过简单的配置步骤,即可在 FastAPI 中启用模板引擎,显著改善用户体验。
104 1
|
13天前
|
数据处理 Python
Django视图:构建动态Web页面的核心技术
Django视图:构建动态Web页面的核心技术
|
1月前
|
XML JavaScript 测试技术
Web自动化测试框架(基础篇)--HTML页面元素和DOM对象
本文为Web自动化测试入门指南,介绍了HTML页面元素和DOM对象的基础知识,以及如何使用Python中的Selenium WebDriver进行元素定位、操作和等待机制,旨在帮助初学者理解Web自动化测试中的关键概念和操作技巧。
38 1
|
1月前
|
缓存 运维 网络协议
一台新PC进行Web页面请求的历程:技术深度剖析
【8月更文挑战第24天】在当今数字化时代,当我们轻轻点击浏览器上的一个链接,背后其实经历了一场复杂而精妙的交互过程。本文将带您深入探索,从一台全新PC的角度出发,揭秘Web页面请求的全过程,展现这背后隐藏的技术奥秘。
28 0
|
1月前
【Azure 应用服务】Web.config中设置域名访问限制,IP地址限制访问特定的页面资源 (Rewrite)
【Azure 应用服务】Web.config中设置域名访问限制,IP地址限制访问特定的页面资源 (Rewrite)