基站定位原理:通过手机信号获取基站信息,然后调用第三方公开的根据基站信息查找基站的经纬度值及地址信息(大概位置)。

一、通过手机信号获取基站信息(详细的可以参考:Android基站定位——通过手机信号获取基站信息(一))

[java] view plain copy
  1. TelephonyManagermTelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  2. //返回值MCC+MNC
  3. Stringoperator=mTelephonyManager.getNetworkOperator();
  4. mcc=Integer.parseInt(operator.substring(0,3));
  5. mnc=Integer.parseInt(operator.substring(3));
  6. //中国移动和中国联通获取LAC、CID的方式
  7. GsmCellLocationlocation=(GsmCellLocation)mTelephonyManager.getCellLocation();
  8. lac=location.getLac();
  9. cid=location.getCid();
  10. Log.i(TAG,"MCC="+mcc+"\tMNC="+mnc+"\tLAC="+lac+"\tCID="+cid);

二、调用第三方公开的API(根据基站信息查找基站的经纬度值及地址信息)

1、组拼JSON形式的请求参数

[java] view plain copy
  1. /**
  2. *获取JSON形式的基站信息
  3. *@parammcc移动国家代码(中国的为460)
  4. *@parammnc移动网络号码(中国移动为0,中国联通为1,中国电信为2);
  5. *@paramlac位置区域码
  6. *@paramcid基站编号
  7. *@returnjson
  8. *@throwsJSONException
  9. */
  10. privateStringgetJsonCellPos(intmcc,intmnc,intlac,intcid)throwsJSONException{
  11. JSONObjectjsonCellPos=newJSONObject();
  12. jsonCellPos.put("version","1.1.0");
  13. jsonCellPos.put("host","maps.google.com");
  14. JSONArrayarray=newJSONArray();
  15. JSONObjectjson1=newJSONObject();
  16. json1.put("location_area_code",""+lac+"");
  17. json1.put("mobile_country_code",""+mcc+"");
  18. json1.put("mobile_network_code",""+mnc+"");
  19. json1.put("age",0);
  20. json1.put("cell_id",""+cid+"");
  21. array.put(json1);
  22. jsonCellPos.put("cell_towers",array);
  23. returnjsonCellPos.toString();
  24. }

2、通过HTTP协议网络请求源码:

[plain] view plain copy
  1. requestURL:http://www.minigps.net/minigps/map/google/location
  2. RequestMethod:POST
  3. StatusCode:200OK
  4. RequestHeadersviewsource
  5. Accept:application/json,text/javascript,*/*;q=0.01
  6. Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
  7. Accept-Encoding:gzip,deflate,sdch
  8. Accept-Language:zh-CN,zh;q=0.8
  9. Connection:keep-alive
  10. Content-Length:191
  11. Content-Type:application/json;charset=UTF-8
  12. Cookie:bdshare_firstime=1356366713546;JSESSIONID=68243935CD3355089CF07A3A22AAB372
  13. Host:www.minigps.net
  14. Origin:http://www.minigps.net
  15. Referer:http://www.minigps.net/map.html
  16. User-Agent:Mozilla/5.0(WindowsNT5.1)AppleWebKit/537.4(KHTML,likeGecko)Chrome/22.0.1229.94Safari/537.4
  17. X-Requested-With:XMLHttpRequest
  18. RequestPayload
  19. {"cell_towers":[{"mobile_network_code":"1","location_area_code":"43018","cell_id":"11152773","age":0,"mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}
  20. ResponseHeadersviewsource
  21. Content-Type:application/json
  22. Date:Sat,03Jan201314:03:15GMT
  23. Server:Apache-Coyote/1.1
  24. Transfer-Encoding:chunked

3、用JAVA代码具体实现:

[java] view plain copy
  1. /**
  2. *调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息
  3. */
  4. publicStringhttpPost(Stringurl,StringjsonCellPos)throwsIOException{
  5. byte[]data=jsonCellPos.toString().getBytes();
  6. URLrealUrl=newURL(url);
  7. HttpURLConnectionhttpURLConnection=(HttpURLConnection)realUrl.openConnection();
  8. httpURLConnection.setConnectTimeout(6*1000);
  9. httpURLConnection.setDoOutput(true);
  10. httpURLConnection.setDoInput(true);
  11. httpURLConnection.setUseCaches(false);
  12. httpURLConnection.setRequestMethod("POST");
  13. httpURLConnection.setRequestProperty("Accept","application/json,text/javascript,*/*;q=0.01");
  14. httpURLConnection.setRequestProperty("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3");
  15. httpURLConnection.setRequestProperty("Accept-Encoding","gzip,deflate,sdch");
  16. httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8");
  17. httpURLConnection.setRequestProperty("Connection","Keep-Alive");
  18. httpURLConnection.setRequestProperty("Content-Length",String.valueOf(data.length));
  19. httpURLConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
  20. httpURLConnection.setRequestProperty("Host","www.minigps.net");
  21. httpURLConnection.setRequestProperty("Referer","http://www.minigps.net/map.html");
  22. httpURLConnection.setRequestProperty("User-Agent",
  23. "Mozilla/5.0(WindowsNT5.1)AppleWebKit/537.4(KHTML,likeGecko)Chrome/22.0.1229.94Safari/537.4X-Requested-With:XMLHttpRequest");
  24. httpURLConnection.setRequestProperty("X-Requested-With","XMLHttpRequest");
  25. httpURLConnection.setRequestProperty("Host","www.minigps.net");
  26. DataOutputStreamoutStream=newDataOutputStream(httpURLConnection.getOutputStream());
  27. outStream.write(data);
  28. outStream.flush();
  29. outStream.close();
  30. if(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
  31. InputStreaminputStream=httpURLConnection.getInputStream();
  32. returnnewString(read(inputStream));
  33. }
  34. returnnull;
  35. }

4、读取返回的JSON数据流代码:

[java] view plain copy
  1. /**
  2. *读取IO流并以byte[]形式存储
  3. *@paraminputSreamInputStream
  4. *@returnbyte[]
  5. *@throwsIOException
  6. */
  7. publicbyte[]read(InputStreaminputSream)throwsIOException{
  8. ByteArrayOutputStreamoutStream=newByteArrayOutputStream();
  9. intlen=-1;
  10. byte[]buffer=newbyte[1024];
  11. while((len=inputSream.read(buffer))!=-1){
  12. outStream.write(buffer,0,len);
  13. }
  14. outStream.close();
  15. inputSream.close();
  16. returnoutStream.toByteArray();
  17. }

三、请求参数及返回结果的JSON形式:

1、请求的JSON参数值:

[plain] view plain copy
  1. {
  2. "cell_towers":
  3. [
  4. {
  5. "mobile_network_code":"1",
  6. "location_area_code":"43018",
  7. "cell_id":"11152773",
  8. "age":0,
  9. "mobile_country_code":"460"
  10. }
  11. ],
  12. "host":"maps.google.com",
  13. "version":"1.1.0"
  14. }

2、返回的JSON结果值:

[plain] view plain copy
  1. {
  2. "location":
  3. {
  4. "latitude":"31.211389541625977",
  5. "longitude":"121.60332489013672",
  6. "address":
  7. {"city":
  8. "上海市浦东新区居里路432号;浦东新区光启安老院、第一三共制药上海公司、SUNPLUS[附近]",
  9. "country":"",
  10. "country_code":"",
  11. "county":"",
  12. "postal_code":"",
  13. "region":"",
  14. "street":"",
  15. "street_number":""
  16. }
  17. },
  18. "access_token":"dummytoken"
  19. }

四、完整代码及所需权限:

Java代码:

[java] view plain copy
  1. packagecom.easipass.test;
  2. importjava.io.ByteArrayOutputStream;
  3. importjava.io.DataOutputStream;
  4. importjava.io.IOException;
  5. importjava.io.InputStream;
  6. importjava.net.HttpURLConnection;
  7. importjava.net.URL;
  8. importorg.json.JSONArray;
  9. importorg.json.JSONException;
  10. importorg.json.JSONObject;
  11. importandroid.app.Activity;
  12. importandroid.content.Context;
  13. importandroid.os.Bundle;
  14. importandroid.telephony.TelephonyManager;
  15. importandroid.telephony.gsm.GsmCellLocation;
  16. importandroid.util.Log;
  17. importandroid.view.View;
  18. /**
  19. *功能描述:单基站定位
  20. *@authorandroid_ls
  21. */
  22. publicclassGSMCellLocationActivityextendsActivity{
  23. privatestaticfinalStringTAG="GSMCellLocationActivity";
  24. privateintmcc;
  25. privateintmnc;
  26. privateintlac;
  27. privateintcid;
  28. @Override
  29. publicvoidonCreate(BundlesavedInstanceState){
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.main);
  32. //获取基站信息
  33. findViewById(R.id.button1).setOnClickListener(newView.OnClickListener(){
  34. @Override
  35. publicvoidonClick(Viewv){
  36. TelephonyManagermTelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  37. //返回值MCC+MNC
  38. Stringoperator=mTelephonyManager.getNetworkOperator();
  39. mcc=Integer.parseInt(operator.substring(0,3));
  40. mnc=Integer.parseInt(operator.substring(3));
  41. //中国移动和中国联通获取LAC、CID的方式
  42. GsmCellLocationlocation=(GsmCellLocation)mTelephonyManager.getCellLocation();
  43. lac=location.getLac();
  44. cid=location.getCid();
  45. Log.i(TAG,"MCC="+mcc+"\tMNC="+mnc+"\tLAC="+lac+"\tCID="+cid);
  46. newThread(){
  47. @Override
  48. publicvoidrun(){
  49. try{
  50. Stringjson=getJsonCellPos(mcc,mnc,lac,cid);
  51. Log.i(TAG,"request="+json);
  52. Stringurl="http://www.minigps.net/minigps/map/google/location";
  53. Stringresult=httpPost(url,json);
  54. Log.i(TAG,"result="+result);
  55. }catch(Exceptione){
  56. //TODOAuto-generatedcatchblock
  57. e.printStackTrace();
  58. }
  59. }
  60. }.start();
  61. }
  62. });
  63. }
  64. /**
  65. *调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息
  66. */
  67. publicStringhttpPost(Stringurl,StringjsonCellPos)throwsIOException{
  68. byte[]data=jsonCellPos.toString().getBytes();
  69. URLrealUrl=newURL(url);
  70. HttpURLConnectionhttpURLConnection=(HttpURLConnection)realUrl.openConnection();
  71. httpURLConnection.setConnectTimeout(6*1000);
  72. httpURLConnection.setDoOutput(true);
  73. httpURLConnection.setDoInput(true);
  74. httpURLConnection.setUseCaches(false);
  75. httpURLConnection.setRequestMethod("POST");
  76. httpURLConnection.setRequestProperty("Accept","application/json,text/javascript,*/*;q=0.01");
  77. httpURLConnection.setRequestProperty("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3");
  78. httpURLConnection.setRequestProperty("Accept-Encoding","gzip,deflate,sdch");
  79. httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8");
  80. httpURLConnection.setRequestProperty("Connection","Keep-Alive");
  81. httpURLConnection.setRequestProperty("Content-Length",String.valueOf(data.length));
  82. httpURLConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
  83. httpURLConnection.setRequestProperty("Host","www.minigps.net");
  84. httpURLConnection.setRequestProperty("Referer","http://www.minigps.net/map.html");
  85. httpURLConnection.setRequestProperty("User-Agent",
  86. "Mozilla/5.0(WindowsNT5.1)AppleWebKit/537.4(KHTML,likeGecko)Chrome/22.0.1229.94Safari/537.4X-Requested-With:XMLHttpRequest");
  87. httpURLConnection.setRequestProperty("X-Requested-With","XMLHttpRequest");
  88. httpURLConnection.setRequestProperty("Host","www.minigps.net");
  89. DataOutputStreamoutStream=newDataOutputStream(httpURLConnection.getOutputStream());
  90. outStream.write(data);
  91. outStream.flush();
  92. outStream.close();
  93. if(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
  94. InputStreaminputStream=httpURLConnection.getInputStream();
  95. returnnewString(read(inputStream));
  96. }
  97. returnnull;
  98. }
  99. /**
  100. *获取JSON形式的基站信息
  101. *@parammcc移动国家代码(中国的为460
  102. *@parammnc移动网络号码(中国移动为0,中国联通为1,中国电信为2);
  103. *@paramlac位置区域码
  104. *@paramcid基站编号
  105. *@returnjson
  106. *@throwsJSONException
  107. */
  108. privateStringgetJsonCellPos(intmcc,intmnc,intlac,intcid)throwsJSONException{
  109. JSONObjectjsonCellPos=newJSONObject();
  110. jsonCellPos.put("version","1.1.0");
  111. jsonCellPos.put("host","maps.google.com");
  112. JSONArrayarray=newJSONArray();
  113. JSONObjectjson1=newJSONObject();
  114. json1.put("location_area_code",""+lac+"");
  115. json1.put("mobile_country_code",""+mcc+"");
  116. json1.put("mobile_network_code",""+mnc+"");
  117. json1.put("age",0);
  118. json1.put("cell_id",""+cid+"");
  119. array.put(json1);
  120. jsonCellPos.put("cell_towers",array);
  121. returnjsonCellPos.toString();
  122. }
  123. /**
  124. *读取IO流并以byte[]形式存储
  125. *@paraminputSreamInputStream
  126. *@returnbyte[]
  127. *@throwsIOException
  128. */
  129. publicbyte[]read(InputStreaminputSream)throwsIOException{
  130. ByteArrayOutputStreamoutStream=newByteArrayOutputStream();
  131. intlen=-1;
  132. byte[]buffer=newbyte[1024];
  133. while((len=inputSream.read(buffer))!=-1){
  134. outStream.write(buffer,0,len);
  135. }
  136. outStream.close();
  137. inputSream.close();
  138. returnoutStream.toByteArray();
  139. }
  140. }

在AndroidManifest.xml添加获取位置信息的权限:

[html] view plain copy
  1. <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
  2. <uses-permissionandroid:name="android.permission.INTERNET"/>

五、测试网址:http://www.minigps.net/map.html

六、Google的基站定位http://www.google.com/loc/json或者http://www.google.com.hk/loc/json已不可用,访问返回404。官方给出的答复:https://developers.google.com/gears/?hl=zh-TW

参考过的博客:echo3博主的json基站定位接口 免费使用

更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  2. 读取android手机流量信息
  3. Android屏幕分辨率正确获取及PX,DPI,DP,SP等的对应关系
  4. android 获取唯一标识
  5. android拍照与读取相册
  6. Android(安卓)热点开关状态的判断和获取热点ssid
  7. Android软键盘适配问题
  8. AIR Native Extension的使用(Android)一 : 打包ane
  9. android之BitMap

随机推荐

  1. android使用webview加载flash文件
  2. Android(安卓)设计一个可单选,多选的Demo
  3. 2010.12.10(4)——— android MapView 处
  4. (转)Android软键盘弹出,界面整体上移
  5. Android(安卓)单元测试cmd 命令集
  6. Android: HowTo设置app不被系统kill掉
  7. Android(安卓)中的全局变量
  8. android之壁纸相关
  9. 运行linux命令
  10. android如何正确获取iccid,特别是中间带有