Android基站定位——单基站定位(二)
16lz
2022-08-05
基站定位原理:通过手机信号获取基站信息,然后调用第三方公开的根据基站信息查找基站的经纬度值及地址信息(大概位置)。
一、通过手机信号获取基站信息(详细的可以参考:Android基站定位——通过手机信号获取基站信息(一))
[java] view plain copy- TelephonyManagermTelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
- //返回值MCC+MNC
- Stringoperator=mTelephonyManager.getNetworkOperator();
- mcc=Integer.parseInt(operator.substring(0,3));
- mnc=Integer.parseInt(operator.substring(3));
- //中国移动和中国联通获取LAC、CID的方式
- GsmCellLocationlocation=(GsmCellLocation)mTelephonyManager.getCellLocation();
- lac=location.getLac();
- cid=location.getCid();
- Log.i(TAG,"MCC="+mcc+"\tMNC="+mnc+"\tLAC="+lac+"\tCID="+cid);
二、调用第三方公开的API(根据基站信息查找基站的经纬度值及地址信息)
1、组拼JSON形式的请求参数
[java] view plain copy- /**
- *获取JSON形式的基站信息
- *@parammcc移动国家代码(中国的为460)
- *@parammnc移动网络号码(中国移动为0,中国联通为1,中国电信为2);
- *@paramlac位置区域码
- *@paramcid基站编号
- *@returnjson
- *@throwsJSONException
- */
- privateStringgetJsonCellPos(intmcc,intmnc,intlac,intcid)throwsJSONException{
- JSONObjectjsonCellPos=newJSONObject();
- jsonCellPos.put("version","1.1.0");
- jsonCellPos.put("host","maps.google.com");
- JSONArrayarray=newJSONArray();
- JSONObjectjson1=newJSONObject();
- json1.put("location_area_code",""+lac+"");
- json1.put("mobile_country_code",""+mcc+"");
- json1.put("mobile_network_code",""+mnc+"");
- json1.put("age",0);
- json1.put("cell_id",""+cid+"");
- array.put(json1);
- jsonCellPos.put("cell_towers",array);
- returnjsonCellPos.toString();
- }
2、通过HTTP协议网络请求源码:
[plain] view plain copy- requestURL:http://www.minigps.net/minigps/map/google/location
- RequestMethod:POST
- StatusCode:200OK
- RequestHeadersviewsource
- Accept:application/json,text/javascript,*/*;q=0.01
- Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
- Accept-Encoding:gzip,deflate,sdch
- Accept-Language:zh-CN,zh;q=0.8
- Connection:keep-alive
- Content-Length:191
- Content-Type:application/json;charset=UTF-8
- Cookie:bdshare_firstime=1356366713546;JSESSIONID=68243935CD3355089CF07A3A22AAB372
- Host:www.minigps.net
- Origin:http://www.minigps.net
- Referer:http://www.minigps.net/map.html
- User-Agent:Mozilla/5.0(WindowsNT5.1)AppleWebKit/537.4(KHTML,likeGecko)Chrome/22.0.1229.94Safari/537.4
- X-Requested-With:XMLHttpRequest
- RequestPayload
- {"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"}
- ResponseHeadersviewsource
- Content-Type:application/json
- Date:Sat,03Jan201314:03:15GMT
- Server:Apache-Coyote/1.1
- Transfer-Encoding:chunked
3、用JAVA代码具体实现:
[java] view plain copy- /**
- *调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息
- */
- publicStringhttpPost(Stringurl,StringjsonCellPos)throwsIOException{
- byte[]data=jsonCellPos.toString().getBytes();
- URLrealUrl=newURL(url);
- HttpURLConnectionhttpURLConnection=(HttpURLConnection)realUrl.openConnection();
- httpURLConnection.setConnectTimeout(6*1000);
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- httpURLConnection.setUseCaches(false);
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setRequestProperty("Accept","application/json,text/javascript,*/*;q=0.01");
- httpURLConnection.setRequestProperty("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3");
- httpURLConnection.setRequestProperty("Accept-Encoding","gzip,deflate,sdch");
- httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8");
- httpURLConnection.setRequestProperty("Connection","Keep-Alive");
- httpURLConnection.setRequestProperty("Content-Length",String.valueOf(data.length));
- httpURLConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
- httpURLConnection.setRequestProperty("Host","www.minigps.net");
- httpURLConnection.setRequestProperty("Referer","http://www.minigps.net/map.html");
- httpURLConnection.setRequestProperty("User-Agent",
- "Mozilla/5.0(WindowsNT5.1)AppleWebKit/537.4(KHTML,likeGecko)Chrome/22.0.1229.94Safari/537.4X-Requested-With:XMLHttpRequest");
- httpURLConnection.setRequestProperty("X-Requested-With","XMLHttpRequest");
- httpURLConnection.setRequestProperty("Host","www.minigps.net");
- DataOutputStreamoutStream=newDataOutputStream(httpURLConnection.getOutputStream());
- outStream.write(data);
- outStream.flush();
- outStream.close();
- if(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
- InputStreaminputStream=httpURLConnection.getInputStream();
- returnnewString(read(inputStream));
- }
- returnnull;
- }
4、读取返回的JSON数据流代码:
[java] view plain copy- /**
- *读取IO流并以byte[]形式存储
- *@paraminputSreamInputStream
- *@returnbyte[]
- *@throwsIOException
- */
- publicbyte[]read(InputStreaminputSream)throwsIOException{
- ByteArrayOutputStreamoutStream=newByteArrayOutputStream();
- intlen=-1;
- byte[]buffer=newbyte[1024];
- while((len=inputSream.read(buffer))!=-1){
- outStream.write(buffer,0,len);
- }
- outStream.close();
- inputSream.close();
- returnoutStream.toByteArray();
- }
三、请求参数及返回结果的JSON形式:
1、请求的JSON参数值:
[plain] view plain copy- {
- "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"
- }
2、返回的JSON结果值:
[plain] view plain copy- {
- "location":
- {
- "latitude":"31.211389541625977",
- "longitude":"121.60332489013672",
- "address":
- {"city":
- "上海市浦东新区居里路432号;浦东新区光启安老院、第一三共制药上海公司、SUNPLUS[附近]",
- "country":"",
- "country_code":"",
- "county":"",
- "postal_code":"",
- "region":"",
- "street":"",
- "street_number":""
- }
- },
- "access_token":"dummytoken"
- }
四、完整代码及所需权限:
Java代码:
[java] view plain copy- packagecom.easipass.test;
- importjava.io.ByteArrayOutputStream;
- importjava.io.DataOutputStream;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.net.HttpURLConnection;
- importjava.net.URL;
- importorg.json.JSONArray;
- importorg.json.JSONException;
- importorg.json.JSONObject;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.os.Bundle;
- importandroid.telephony.TelephonyManager;
- importandroid.telephony.gsm.GsmCellLocation;
- importandroid.util.Log;
- importandroid.view.View;
- /**
- *功能描述:单基站定位
- *@authorandroid_ls
- */
- publicclassGSMCellLocationActivityextendsActivity{
- privatestaticfinalStringTAG="GSMCellLocationActivity";
- privateintmcc;
- privateintmnc;
- privateintlac;
- privateintcid;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //获取基站信息
- findViewById(R.id.button1).setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- TelephonyManagermTelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
- //返回值MCC+MNC
- Stringoperator=mTelephonyManager.getNetworkOperator();
- mcc=Integer.parseInt(operator.substring(0,3));
- mnc=Integer.parseInt(operator.substring(3));
- //中国移动和中国联通获取LAC、CID的方式
- GsmCellLocationlocation=(GsmCellLocation)mTelephonyManager.getCellLocation();
- lac=location.getLac();
- cid=location.getCid();
- Log.i(TAG,"MCC="+mcc+"\tMNC="+mnc+"\tLAC="+lac+"\tCID="+cid);
- newThread(){
- @Override
- publicvoidrun(){
- try{
- Stringjson=getJsonCellPos(mcc,mnc,lac,cid);
- Log.i(TAG,"request="+json);
- Stringurl="http://www.minigps.net/minigps/map/google/location";
- Stringresult=httpPost(url,json);
- Log.i(TAG,"result="+result);
- }catch(Exceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- }.start();
- }
- });
- }
- /**
- *调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息
- */
- publicStringhttpPost(Stringurl,StringjsonCellPos)throwsIOException{
- byte[]data=jsonCellPos.toString().getBytes();
- URLrealUrl=newURL(url);
- HttpURLConnectionhttpURLConnection=(HttpURLConnection)realUrl.openConnection();
- httpURLConnection.setConnectTimeout(6*1000);
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- httpURLConnection.setUseCaches(false);
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setRequestProperty("Accept","application/json,text/javascript,*/*;q=0.01");
- httpURLConnection.setRequestProperty("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3");
- httpURLConnection.setRequestProperty("Accept-Encoding","gzip,deflate,sdch");
- httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8");
- httpURLConnection.setRequestProperty("Connection","Keep-Alive");
- httpURLConnection.setRequestProperty("Content-Length",String.valueOf(data.length));
- httpURLConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
- httpURLConnection.setRequestProperty("Host","www.minigps.net");
- httpURLConnection.setRequestProperty("Referer","http://www.minigps.net/map.html");
- httpURLConnection.setRequestProperty("User-Agent",
- "Mozilla/5.0(WindowsNT5.1)AppleWebKit/537.4(KHTML,likeGecko)Chrome/22.0.1229.94Safari/537.4X-Requested-With:XMLHttpRequest");
- httpURLConnection.setRequestProperty("X-Requested-With","XMLHttpRequest");
- httpURLConnection.setRequestProperty("Host","www.minigps.net");
- DataOutputStreamoutStream=newDataOutputStream(httpURLConnection.getOutputStream());
- outStream.write(data);
- outStream.flush();
- outStream.close();
- if(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
- InputStreaminputStream=httpURLConnection.getInputStream();
- returnnewString(read(inputStream));
- }
- returnnull;
- }
- /**
- *获取JSON形式的基站信息
- *@parammcc移动国家代码(中国的为460)
- *@parammnc移动网络号码(中国移动为0,中国联通为1,中国电信为2);
- *@paramlac位置区域码
- *@paramcid基站编号
- *@returnjson
- *@throwsJSONException
- */
- privateStringgetJsonCellPos(intmcc,intmnc,intlac,intcid)throwsJSONException{
- JSONObjectjsonCellPos=newJSONObject();
- jsonCellPos.put("version","1.1.0");
- jsonCellPos.put("host","maps.google.com");
- JSONArrayarray=newJSONArray();
- JSONObjectjson1=newJSONObject();
- json1.put("location_area_code",""+lac+"");
- json1.put("mobile_country_code",""+mcc+"");
- json1.put("mobile_network_code",""+mnc+"");
- json1.put("age",0);
- json1.put("cell_id",""+cid+"");
- array.put(json1);
- jsonCellPos.put("cell_towers",array);
- returnjsonCellPos.toString();
- }
- /**
- *读取IO流并以byte[]形式存储
- *@paraminputSreamInputStream
- *@returnbyte[]
- *@throwsIOException
- */
- publicbyte[]read(InputStreaminputSream)throwsIOException{
- ByteArrayOutputStreamoutStream=newByteArrayOutputStream();
- intlen=-1;
- byte[]buffer=newbyte[1024];
- while((len=inputSream.read(buffer))!=-1){
- outStream.write(buffer,0,len);
- }
- outStream.close();
- inputSream.close();
- returnoutStream.toByteArray();
- }
- }
在AndroidManifest.xml添加获取位置信息的权限:
[html] view plain copy- <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <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基站定位接口 免费使用
更多相关文章
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
- 读取android手机流量信息
- Android屏幕分辨率正确获取及PX,DPI,DP,SP等的对应关系
- android 获取唯一标识
- android拍照与读取相册
- Android(安卓)热点开关状态的判断和获取热点ssid
- Android软键盘适配问题
- AIR Native Extension的使用(Android)一 : 打包ane
- android之BitMap