概要:在Android系統中,能夠用網絡定位,GPS定位,還有基站定位,固然GPS定位是最爲精確的定位,可是GPS定位由於是依靠衛星定位,因此定位所需時間長,並且依賴於外界條件,如雲層過厚也沒法取得GPS信號。基站定位速度是很快的,致命的缺點是定位不精確。可是基於有些特殊的要求,這種定位也非徹底無用。這裏簡單的介紹一下如何使用基站定位。 javascript
基站定位大體思想是,從TelephonyManager中取得手機的 MCC(Mobile Country Code),MNC(Mobile Net Code),LAC(Location Area Code),Cell ID信息,而後從已知的數據庫中檢索並獲取位置信息。 html
曾經能夠從Google Gears獲取基站信息,可是2012年,Google關閉了此服務的免費服務,改成收費服務了。google了一下,發現有一個網址能夠查詢基站信息,並且這個網站利用的是Google Gears服務。下面介紹如何使用這個網站獲取基站信息。 java
1.訪問WebSite android
http://www.minigps.net/map.html
git
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庫) json
3.編碼 網絡
3-1.取得手機信息 app
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); post
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; } } }