Android base station positioning source code

Source: Internet
Author: User

After several days of research and testing, we finally solved the positioning code for the base stations of China Unicom 2G, China Mobile 2G, and China Telecom 3G. The team only has the standard of these machines. Let me give you a detailed explanation.
1. Related technical content
TelephonyManager Management in Google Android APIs.
Code differences between different standards of China Unicom, China Mobile, and China Telecom in obtaining the location of the base station.
Use the basic information of the base station and Google Gears to obtain the corresponding GPS longitude and latitude.
Use the Google Map API to obtain the current location based on the GPS longitude and latitude.

2. Current Problems
Because the obtained GPS longitude and latitude are displayed on Google Map and the offset is required, this section is not processed yet.
PhoneStateListener is not used to update the status in real time.
No threads are used to obtain data asynchronously.
No service is used to obtain data in real time
Therefore, for commercial use, further modification is required.
3 of course, this part of the code has been transplanted to our Home Guard project, and all the problems mentioned in 2 have been solved.
Below I will describe the code for the four main contents in the first part.
1. TelephonyManager Management in Google Android APIs.
 
TelephonyManager tm = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE );

In this way, you can obtain the TelephonyManager interface.
The source code of this interface can be viewed by setting it in the project. It is not attached here.
After TelephonyManager is obtained, the code is different for different carriers, so you need to determine getNetworkType ()
The following types are defined in the source code:
/** Network type is unknown */
Public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
Public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
Public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
Public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B */
Public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0 */
Public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision */
Public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT */
Public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
Public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
Public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
Public static final int NETWORK_TYPE_HSPA = 10;

2. Code differences between different standards of China Unicom, China Mobile, and China Telecom in obtaining the location of the base station.
This part was actually tested by me. After countless disconnections and cards, we achieved the perfect implementation of different standards.
The Code is as follows:
TelephonyManager tm = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE );
Int type = tm. getNetworkType ();
// China Telecom is CTC
// NETWORK_TYPE_EVDO_A is the getNetworkType of China Telecom 3G.
// NETWORK_TYPE_CDMA China Telecom 2G is CDMA
If (type = TelephonyManager. NETWORK_TYPE_EVDO_A | type = TelephonyManager. NETWORK_TYPE_CDMA | type = TelephonyManager. NETWORK_TYPE_1xRTT)
{
}
// Mobile 2G card + CMCC + 2
// Type = NETWORK_TYPE_EDGE
Else if (type = TelephonyManager. NETWORK_TYPE_EDGE)
{
}
// China Unicom's 2G network tested China Unicom 1 NETWORK_TYPE_GPRS
Else if (type = TelephonyManager. NETWORK_TYPE_GPRS)
{
 
}
Else
{
TV. setText ("Current Not Support This Type .");
}

3. Use the basic information of the base station and Google Gears to obtain the corresponding GPS longitude and latitude.
As mentioned in the previous two articles, the code references the code of netizens. Thank you.
Private Location callGear (ArrayList cellID ){
If (cellID = null) return null;
DefaultHttpClient client = new DefaultHttpClient ();
HttpPost post = new HttpPost (
Http://www.google.com/loc/json ");
JSONObject holder = new JSONObject ();
Try {
Holder. put ("version", "1.1.0 ");
Holder. put ("host", "maps.google.com ");
Holder. put ("home_mobile_country_code", cellID. get (0). mobileCountryCode );
Holder. put ("home_mobile_network_code", cellID. get (0). deleenetworkcode );
Holder. put ("radio_type", cellID. get (0). radioType );
Holder. put ("request_address", true );
If ("460". equals (cellID. get (0). mobileCountryCode ))
Holder. put ("address_language", "zh_CN ");
Else
Holder. put ("address_language", "en_US ");
JSONObject data, current_data;
JSONArray array = new JSONArray ();
Current_data = new JSONObject ();
Current_data.put ("cell_id", cellID. get (0). cellId );
Current_data.put ("location_area_code", cellID. get (0). locationAreaCode );
Current_data.put ("mobile_country_code", cellID. get (0). mobileCountryCode );
Current_data.put ("mobile_network_code", cellID. get (0). mobileNetworkCode );
Current_data.put ("age", 0 );
Array. put (current_data );
If (cellID. size ()> 2 ){
For (int I = 1; I <cellID. size (); I ++ ){
Data = new JSONObject ();
Data. put ("cell_id", cellID. get (I). cellId );
Data. put ("location_area_code", cellID. get (I). locationAreaCode );
Data. put ("mobile_country_code", cellID. get (I). mobileCountryCode );
Data. put ("mobile_network_code", cellID. get (I). mobileNetworkCode );
Data. put ("age", 0 );
Array. put (data );
}
}
Holder. put ("cell_towers", array );
StringEntity se = new StringEntity (holder. toString ());
Log. e ("Location send", holder. toString ());
Post. setEntity (se );
HttpResponse resp = client.exe cute (post );
HttpEntity entity = resp. getEntity ();
 
BufferedReader br = new BufferedReader (
New InputStreamReader (entity. getContent ()));
StringBuffer sb = new StringBuffer ();
String result = br. readLine ();
While (result! = Null ){
Log. e ("Locaiton receive", result );
Sb. append (result );
Result = br. readLine ();
}
If (sb. length () return null;
Data = new JSONObject (sb. toString ());
Data = (JSONObject) data. get ("location ");
 
Location loc = new Location (LocationManager. NETWORK_PROVIDER );
Loc. setLatitude (Double) data. get ("latitude "));
Loc. setlongpolling (Double) data. get ("longpolling "));
Loc. setAccuracy (Float. parseFloat (data. get ("accuracy"). toString ()));
Loc. setTime (GetUTCTime ());
Return loc;
} Catch (JSONException e ){
Return null;
} Catch (UnsupportedEncodingException e ){
E. printStackTrace ();
} Catch (ClientProtocolException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return null;
}

4. Use the Google Map API to obtain the current location based on the GPS longitude and latitude.
This part of the Code refers to the simple base station positioning program and thanks to Brother lei for such a good article. At the same time, Lei yixiong's layout is really nice and clear.
 
Private String getLocation (Location failed) throws Exception {
String resultString = "";
 
/** Use the get method here to add parameters directly to the URL */
String urlString = String. format ("http://maps.google.cn/maps/geo? Key = abcdefg & q = % s, % s ", response. getLatitude (), response. getlongdistance ());
Log. I ("URL", urlString );
 
/** Create HttpClient */
HttpClient client = new DefaultHttpClient ();
/** Use the GET Method */
HttpGet = new HttpGet (urlString );
Try {
/** Initiate a GET request and obtain the returned data */
HttpResponse response = client.exe cute (get );
HttpEntity entity = response. getEntity ();
BufferedReader buffReader = new BufferedReader (new InputStreamReader (entity. getContent ()));
StringBuffer strBuff = new StringBuffer ();
String result = null;
While (result = buffReader. readLine ())! = Null ){
StrBuff. append (result );
}
ResultString = strBuff. toString ();
 
/** Parse JSON data to obtain the physical address */
If (resultString! = Null & resultString. length ()> 0 ){
JSONObject jsonobject = new JSONObject (resultString );
JSONArray jsonArray = new JSONArray (jsonobject. get ("Placemark"). toString ());
ResultString = "";
For (int I = 0; I <jsonArray. length (); I ++ ){
ResultString = jsonArray. getJSONObject (I). getString ("address ");
}
}
} Catch (Exception e ){
Throw new Exception ("An error occurred while obtaining the physical location:" + e. getMessage ());
} Finally {
Get. abort ();
Client = null;
}
 
Return resultString;
}

 
5. The most important thing is to attach the code.
AndroidPosition
Add:
In AndroidMenifest. xml, you must add
Android. permission. INTERNET, android. permission. ACCESS_COARSE_LOCATION, and android. permission. READ_PHONE_STATE permissions. Otherwise, an error occurs.
Put it in front of the Application package.
 
6. Check the effect.

 

 

7. When submitting data to Google Gears, the format is as follows:
Data format sent to Google:
02-24 18:08:20. 550: E/Location send (12892): {"address_language": "zh_CN", "host": "maps.google.com", "radio_type": "cdma", "home_mobile_country_code ": "460", "home_mobile_network_code": "13965", "cell_towers": [{"mobile_network_code": "13965", "location_area_code": 11, "cell_id": 1985, "age": 0, "mobile_country_code": "460"}], "request_address": true, "version": "1.1.0 ″}
Received Google data format:
02-24 18:08:22. 975: E/Locaiton receive (12892): {"location": {"latitude": 43.8595097, "longpolling": 125.3355736, "address": {"country ": "China", "country_code": "CN", "region": "Jilin Province", "city": "Changchun city", "street": "Wenchang road ", "street_number": ""}, "accuracy": 1815.0}, "access_token": "2: _ Kpk9mOFMgyWgLai: 8iWlDpBYZsp4_VxO "}
-End-

 

From mobile development team

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.