当前位置: 首页 > news >正文

Android --- 基站定位

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

概要:在Android系统中,可以用网络定位,GPS定位,还有基站定位,当然GPS定位是最为精确的定位,但是GPS定位因为是依靠卫星定位,所以定位所需时间长,而且依赖于外界条件,如云层过厚也无法取得GPS信号。基站定位速度是很快的,致命的缺点是定位不精确。但是基于有些特殊的要求,这种定位也非完全无用。这里简单的介绍一下如何使用基站定位。

基站定位大致思想是,从TelephonyManager中取得手机的 MCC(Mobile Country Code),MNC(Mobile Net Code),LAC(Location Area Code),Cell ID信息,然后从已知的数据库中检索并获取位置信息。

曾经可以从Google Gears获取基站信息,但是2012年,Google关闭了此服务的免费服务,改为收费服务了。google了一下,发现有一个网址可以查询基站信息,而且这个网站利用的是Google Gears服务。下面介绍如何使用这个网站获取基站信息。

1.访问WebSite

http://www.minigps.net/map.html

1.权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
2.外部库

gson2.2(因为网络返回的结果为Json格式,因个人喜好,故使用了Google的Json库)

3.编码

3-1.取得手机信息

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

String strmcc = tm.getNetworkOperator(); Log.v("Jumper", strmcc); mcc = Integer.parseInt(strmcc.substring(0, 3)); mnc = Integer.parseInt(strmcc.substring(3)); lac = 0; cellid = 0; if(tm.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) { CdmaCellLocation location = (CdmaCellLocation)tm.getCellLocation(); cellid = location.getBaseStationId(); lac = location.getNetworkId(); } else { GsmCellLocation location = (GsmCellLocation)tm.getCellLocation(); cellid = location.getCid(); lac = location.getLac(); }

3-2.获取基站信息

从minigps中获取基站信息需要设置下列的参数。

//URL:http://www.minigps.net/minigps/map/google/location
//	   Request Method:POST
//	   Status Code:200 OK
//	   Request Headersview source
//	   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 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko)            Chrome/22.0.1229.94 Safari/537.4
//	   X-Requested-With:XMLHttpRequest
//	   Request Payload
//	   {"version":"1.1.0","host":"maps.google.com","cell_towers":  [{"cell_id":"3721","location_area_code":"9779","mobile_country_code":"460","mobile_network_c       ode":"0","age":0,"signal_strength":-65}]}
//	    Response Headersview source
//	   Content-Type:application/json
//	   Date:Sat, 12 Jan 2013 06:03:15 GMT
//	   Server:Apache-Coyote/1.1
//	   Transfer-Encoding:chunked

获取基站信息源代码:

package com.jumper.android.demos.location;

import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class MiniGPSConnector {
	private int mMcc;
	private int mMnc;
	private int mLac;
	private int mCellid;

	private final static String MINIGPS_URL = "http://www.minigps.net/minigps/map/google/location";
	
	public MiniGPSConnector(int mcc, int mnc, int lac, int cellid) {
		mMcc = mcc;
		mMnc = mnc;
		mLac = lac;
		mCellid = cellid;
	}

	private JSONObject getPostInfo() {
		JSONObject root = new JSONObject();
		try {
			root.put("version", "1.1.0");
			root.put("host", "maps.google.com");

			JSONArray cell_towers = new JSONArray();
			JSONObject cell_tower = new JSONObject();

			cell_tower.put("cell_id", mCellid);
			cell_tower.put("location_area_code", mLac);
			cell_tower.put("mobile_country_code", mMcc);
			cell_tower.put("mobile_network_code", mMnc);
			cell_tower.put("request_address", true);
			if(mMcc == 460) {
				cell_tower.put("address_language", Locale.CHINA);
			}
			else {
				cell_tower.put("address_language", Locale.US);
			}
			//cell_tower.put("address_language", "zh_CN");
			cell_tower.put("age", 0);

			cell_towers.put(cell_tower);
			root.put("cell_towers", cell_towers);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return root;
	}

	public JsonMiniGPS getMiniGPS() {
		JsonMiniGPS ret = null;
		JSONObject root = getPostInfo();
		if(root == null) {
			return ret;
		}
		
		try {
			URL url = new URL(MINIGPS_URL);
			HttpURLConnection httpURLConnection = (HttpURLConnection) url.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(root.toString().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 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4X-Requested-With:XMLHttpRequest");

			httpURLConnection.setRequestProperty("X-Requested-With",
					"XMLHttpRequest");
			httpURLConnection.setRequestProperty("Host", "www.minigps.net");

			DataOutputStream outStream = new DataOutputStream(
					httpURLConnection.getOutputStream());
			outStream.write(root.toString().getBytes());
			outStream.flush();
			outStream.close();

			if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream inputStream = httpURLConnection.getInputStream();
				Gson gson = new GsonBuilder().create();
				ret = gson.fromJson(new InputStreamReader(inputStream), JsonMiniGPS.class);
				inputStream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
}
返回结果Json格式定义:

package com.jumper.android.demos.location;

public class JsonMiniGPS {

	private String access_token = null;
	private MiniGPSLocation location = null;
	
	public String getAccess_token() {
		return access_token;
	}

	public void setAccess_token(String access_token) {
		this.access_token = access_token;
	}

	public MiniGPSLocation getLocation() {
		return location;
	}

	public void setLocation(MiniGPSLocation location) {
		this.location = location;
	}

	public class MiniGPSLocation {
		private String latitude = null;
		private String longitude = null;
		private MiniGPSAddress address = null;
		public String getLatitude() {
			return latitude;
		}
		public void setLatitude(String latitude) {
			this.latitude = latitude;
		}
		public String getLongitude() {
			return longitude;
		}
		public void setLongitude(String longitude) {
			this.longitude = longitude;
		}
		public MiniGPSAddress getAddress() {
			return address;
		}
		public void setAddress(MiniGPSAddress address) {
			this.address = address;
		}
		
	}
	
	public class MiniGPSAddress {
		private String city = null;
		private String country = null;
		private String country_code = null;
		private String county = null;
		private String postal_code = null;
		private String region = null;
		private String street = null;
		private String street_number = null;
		public String getCity() {
			return city;
		}
		public void setCity(String city) {
			this.city = city;
		}
		public String getCountry() {
			return country;
		}
		public void setCountry(String country) {
			this.country = country;
		}
		public String getCountry_code() {
			return country_code;
		}
		public void setCountry_code(String country_code) {
			this.country_code = country_code;
		}
		public String getCounty() {
			return county;
		}
		public void setCounty(String county) {
			this.county = county;
		}
		public String getPostal_code() {
			return postal_code;
		}
		public void setPostal_code(String postal_code) {
			this.postal_code = postal_code;
		}
		public String getRegion() {
			return region;
		}
		public void setRegion(String region) {
			this.region = region;
		}
		public String getStreet() {
			return street;
		}
		public void setStreet(String street) {
			this.street = street;
		}
		public String getStreet_number() {
			return street_number;
		}
		public void setStreet_number(String street_number) {
			this.street_number = street_number;
		}
	}
}

转载于:https://my.oschina.net/u/1021301/blog/120986

相关文章:

  • TCP传输控制协议分析
  • linux下安装java,ant,maven,git
  • bootstrap-徽章-链接
  • centos下编译安装nginx,并增加nginx_upstream_check_module模块
  • LOD层次细节算法-大规模实时地形的绘制
  • oracle10g创建用户
  • 【体系结构】MySQL 日志文件--慢查询日志
  • java注解[转]
  • Notepad++使用技法
  • 12月21日 特殊权限与软、硬链接文件
  • jquery.pagination.js分页插件的运用
  • 宋体、变量-Oracle存储过程基本语法-by小雨
  • Allot流量控制系统软件升级过程
  • FTP服务器配置与管理(2) 创建FTP站点
  • 局域网介质访问控制方法
  • (三)从jvm层面了解线程的启动和停止
  • 230. Kth Smallest Element in a BST
  • Material Design
  • session共享问题解决方案
  • Spring核心 Bean的高级装配
  • 阿里云应用高可用服务公测发布
  • 爱情 北京女病人
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 使用 5W1H 写出高可读的 Git Commit Message
  • 使用SAX解析XML
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • 我与Jetbrains的这些年
  • 赢得Docker挑战最佳实践
  • 优秀架构师必须掌握的架构思维
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • Prometheus VS InfluxDB
  • Spring Batch JSON 支持
  • 阿里云重庆大学大数据训练营落地分享
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • 我们雇佣了一只大猴子...
  • ​低代码平台的核心价值与优势
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • # 达梦数据库知识点
  • #DBA杂记1
  • #if 1...#endif
  • (003)SlickEdit Unity的补全
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (第一天)包装对象、作用域、创建对象
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (十)c52学习之旅-定时器实验
  • (新)网络工程师考点串讲与真题详解
  • (转)Google的Objective-C编码规范
  • (转)socket Aio demo
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .htaccess配置重写url引擎
  • .Mobi域名介绍
  • .Net Redis的秒杀Dome和异步执行
  • @ComponentScan比较
  • [ MSF使用实例 ] 利用永恒之蓝(MS17-010)漏洞导致windows靶机蓝屏并获取靶机权限