2019独角兽企业重金招聘Python工程师标准>>>
概要:在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;
}
}
}