源代码地址: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>

转载于:https://www.cnblogs.com/psunny/archive/2010/04/17/1714427.html

通过Web页面获取基站位置(Web端,源码下载)相关推荐

  1. web前端的十种jquery特效及源码下载

    1.纯CSS3实现自定义Tooltip边框 涂鸦风格 这是一款用纯CSS3打造的自定义Tooltip边框的应用,之前我们讨论过如何用CSS3来实现不同样式的Tooltip,今天的这款Tooltip却可 ...

  2. 【开源项目】使用环信SDK搭建在线教学场景(含三端源码下载)

    2021年在线教育行业如火如荼,所谓人人为我,我为人人,为了方便教育行业的小伙伴们更好地使用环信SDK,我搭建了一个在线教学开源项目"环环教育",一期覆盖1对1互动教学.在线互动小 ...

  3. java web编写的在线问卷系统 完整源码 下载直接运行

    今天为大家分享一个java web编写的在线问卷系统,目前系统功能已经完善,后续会进一步完善.整个系统界面漂亮,有完整得源码,希望大家可以喜欢.喜欢的帮忙点赞和关注.一起编程.一起进步. 开发环境 开 ...

  4. java开发_mysql中获取数据库表描述_源码下载

    功能描述: 在mysql数据库中,有两张表: data_element_config , test_table 我们需要获取表:test_table表的描述信息,然后把描述信息插入到表:data_el ...

  5. 全云端万能小程序_万能门店全云端独立版微信小程序源码V4.0.10,全五端源码下载...

    独立版万能门店全端云小程序是一款可以一键生成全端小程序(微信小程序.百度小程序.支付宝小程序.抖音/头条小程序.QQ 小程序)的平台, 平台集成了分销商城.会员卡券.知识付费.商户平台.同城论坛等功能 ...

  6. 用python写排课系统_JSP基于WEB的网上选排课系统,源码下载

    大家好,我是全微毕设团队的创始人,本团队擅长JAVA(SSM,SSH,SPRINGBOOT).PYTHON.PHP.C#.安卓等多项技术. 今天将为大家分析一个网上选排课系统(高校排课系统要求十分严格 ...

  7. 赞!超炫的页面切换动画效果【附源码下载】

    在下面的示例中罗列了一组动画,可以被应用到页面切换过程中,创造出很有趣的导航效果.虽然有些效果都非常简单,只是简单的滑动动作,但另外的一些则是利用了视角(Perspective)和 3D 转换(3D ...

  8. 微信小程序动态更改标题栏_微信小程序实现动态设置页面标题的方法【附源码下载】...

    本文实例讲述了微信小程序实现动态设置页面标题的方法.分享给大家供大家参考,具体如下: 1.效果展示 2.关键代码 ① WXML文件 标题1 标题2 标题3 还原 ② JS文件 Page({ // 设置 ...

  9. php网络验证系统源码,kakaPHP 网络验证PHP服务端源码 - 下载 - 搜珍网

    卡卡PHP/卡卡PHP/config/config.php 卡卡PHP/卡卡PHP/config/index.html 卡卡PHP/卡卡PHP/includes/controller/checkgoo ...

  10. 基于Java毕业设计校园外卖系统Web端源码+系统+mysql+lw文档+部署软件

    基于Java毕业设计校园外卖系统Web端源码+系统+mysql+lw文档+部署软件 基于Java毕业设计校园外卖系统Web端源码+系统+mysql+lw文档+部署软件 本源码技术栈: 项目架构:B/S ...

最新文章

  1. SAP MM 采购价格里的阶梯价格
  2. “汇新杯”新兴科技+互联网创新大赛青年创客专项赛决赛
  3. 架构师之路 — 分布式系统 — gRPC 谷歌远程过程调用
  4. html5登录界面源代码_TwinCAT HMI,一款基于web的人机界面产品
  5. 通过设置Cookie 让弹框显示一次
  6. java delphi 三层_三层架构delphi+Java+Oracle模式的实现
  7. 一流程序员靠数学,二流程序员靠算法,低端看高端就是黑魔法
  8. Linux下企业级分区方案
  9. php double 类型 浮点数相减
  10. CES:IT大变革,软件的新平台与新机遇
  11. [Java] 蓝桥杯ALGO-2 算法训练 最大最小公倍数
  12. 使用SSH从服务器下载文件
  13. Hybird App开发,懂得小程序+kbone+finclip就够了!
  14. 在centos里面安装配置caddy
  15. 怎么看xp计算机是32位还是64位,教你查看XP系统的不同32位还是64位详细的步骤
  16. conda 查看已有环境
  17. 视觉 数据_视觉数据讲故事的力量
  18. linux上运行gfortran,linux – gfortran:在64位系统中编译32位可执行文件
  19. 最全的网站推广方案(SEO)
  20. 【操作系统】CSAPP学习笔记

热门文章

  1. 现任明教教主 NAC Framework EOU 视频
  2. [RHCE033]unit9vim工具的使用
  3. 毕业后半年就变成了一条“狗
  4. poj 1961 Period kmp基础
  5. ​​​​SSH Config Editor Pro :管理您的SSH配置文件
  6. 在Mac上使用Charles抓包总是unknown
  7. iOS开发之UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout
  8. 在苹果Mac上格式化USB闪存驱动器
  9. 推荐:MacBook如何快速添加指纹!
  10. 使用Movavi Video Editor如何做局部的影片放大特效