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

简介:

 

源代码地址: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如需转载请自行联系原作者
相关文章
|
JSON 人工智能 前端开发
用markdown语法制作一个好看的网址导航页面(markdown-web-nav)
这是一篇关于创建网址导航页面的工具分享文章。作者介绍了从手动编写HTML代码到开发可视化工具 *markdown-web-nav* 的历程,旨在简化网址管理与导航页面生成的过程。该工具支持新增、编辑和删除网址数据,通过导入/导出JSON文件、实时预览Markdown效果以及一键复制等功能,让用户轻松制作美观的网站导航页面。文章还提供了详细的操作步骤及常见问题解答,如还原数据、获取网站图标链接等,适合不同技术水平的用户使用。
935 28
|
移动开发 前端开发 JavaScript
H5 页面与 Web 页面的制作方法
H5页面制作利用HTML5、CSS3和JavaScript技术,结合H5编辑器或框架(如Adobe Dreamweaver、Ionic),注重移动设备兼容性与响应式布局。Web页面制作则基于传统HTML、CSS和JavaScript,借助文本编辑器或IDE完成开发。两者区别在于技术版本、交互性和浏览器支持:H5更互动、现代,但可能不兼容旧浏览器;Web页面更静态、兼容性广。根据需求选择:高交互选H5,广泛兼容选Web。
2169 6
|
前端开发
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
1413 1
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
|
编解码 前端开发 JavaScript
使用 CSS 打印样式为 Web 页面设置专业的打印机效果
使用 CSS 打印样式为 Web 页面设置专业的打印机效果
793 2
|
存储
在 Web 中判断页面是不是刷新
【9月更文挑战第10天】在Web开发中,判断页面是否刷新有多种方法:1) 监听`popstate`事件,检测用户是否通过历史记录访问页面;2) 记录并比较页面加载时间戳,若相差极小,则可能为刷新;3) 利用本地存储设置特定值,若该值不存在或不符合预期,则页面可能被刷新。然而,这些方法并非绝对准确。
983 3
|
负载均衡 网络协议 应用服务中间件
web群集--rocky9.2源码部署nginx1.24的详细过程
Nginx 是一款由 Igor Sysoev 开发的开源高性能 HTTP 服务器和反向代理服务器,自 2004 年发布以来,以其高效、稳定和灵活的特点迅速成为许多网站和应用的首选。本文详细介绍了 Nginx 的核心概念、工作原理及常见使用场景,涵盖高并发处理、反向代理、负载均衡、低内存占用等特点,并提供了安装配置教程,适合开发者参考学习。
594 1
|
数据处理 开发者 UED
FastAPI 的模板引擎简直太神奇啦!这就是构建动态 Web 页面的终极秘籍,快来一探究竟!
【8月更文挑战第31天】FastAPI 是一款高性能异步 Web 框架,可通过集成模板引擎(如 Jinja2 或 Mako)实现动态页面渲染。使用模板引擎可分离页面结构与数据,简化代码并提升可维护性。此外,它还提供丰富的语法支持,如循环和条件判断,从而增强页面展示效果及开发效率。通过简单的配置步骤,即可在 FastAPI 中启用模板引擎,显著改善用户体验。
1861 2
html,web页面朗读文字,朗读中文,朗读英文
html,web页面朗读文字,朗读中文,朗读英文
|
数据处理 Python
Django视图:构建动态Web页面的核心技术
Django视图:构建动态Web页面的核心技术
【Azure 应用服务】Web.config中设置域名访问限制,IP地址限制访问特定的页面资源 (Rewrite)
【Azure 应用服务】Web.config中设置域名访问限制,IP地址限制访问特定的页面资源 (Rewrite)
396 0

热门文章

最新文章