Simple base station locating program simple base station locating Program

Source: Internet
Author: User

The simple base station positioning program describes how to obtain the current base station location in detail. The following is a brief summary:

 

In the Android operating system, base station positioning is actually very simple. let's first talk about the implementation process:

Call the API (telephonymanager) in the SDK to obtain information such as MCC, MNC, LAC, and CID, and then use the Google API to obtain the longitude and latitude of the location, finally, the actual geographic location is obtained through the Google map API.

MCC, Mobile country code, and mobile country code (460 in China );

MNC, Mobile network code, mobile network number (China Mobile is 00, China Unicom is 01 );

LAC, Location area code, Location Area Code;

CID, Cell identity, base station number, is a 16-bit data (range: 0 to 65535 ).

Google stores this information and directly queries it to obtain the longitude and latitude.

 

Obtain Base Station Information
/**
* Obtain Base Station Information
*
* @ Throws exception
*/
Private scell getcellinfo () throws exception {
Scell cell = new scell ();

/** Call the API to obtain the information of the base station */
Telephonymanager mtelnet = (telephonymanager) getsystemservice (context. telephony_service );
Gsmcelllocation location = (gsmcelllocation) mtelnet. getcelllocation ();
If (location = NULL)
Throw new exception ("An error occurred while obtaining the base station information ");

String operator = mtelnet. getnetworkoperator ();
Int MCC = integer. parseint (operator. substring (0, 3 ));
Int MNC = integer. parseint (operator. substring (3 ));
Int cid = location. getcid ();
Int LAC = location. getlac ();

/** Put the obtained data in the struct */
Cell. MCC = MCC;
Cell. MNC = MNC;
Cell. LAC = LAC;
Cell. cid = CID;

Return cell;
}

 

Obtain longitude and latitude
/**
* Obtain the longitude and latitude
*
* @ Throws exception
*/
Private situde getitude (scell cell) throws exception {
S00000000= new s0000 ();

/** Use the default httpclient of Android */
Httpclient client = new defaulthttpclient ();
/** Use the post Method */
Httppost post = new httppost ("http://www.google.com/loc/json ");
Try {
/** Construct the JSON data of post */
Jsonobject holder = new jsonobject ();
Holder. Put ("version", "1.1.0 ");
Holder. Put ("host", "maps.google.com ");
Holder. Put ("address_language", "zh_cn ");
Holder. Put ("request_address", true );
Holder. Put ("radio_type", "GSM ");
Holder. Put ("carrier", "HTC ");

Jsonobject Tower = new jsonobject ();
Tower. Put ("mobile_country_code", cell. MCC );
Tower. Put ("mobile_network_code", cell. MNC );
Tower. Put ("cell_id", cell. CID );
Tower. Put ("location_area_code", cell. LAC );

Jsonarray towerarray = new jsonarray ();
Towerarray. Put (Tower );
Holder. Put ("cell_towers", towerarray );

Stringentity query = new stringentity (holder. tostring ());
Post. setentity (query );

/** Send post data and obtain returned data */
Httpresponse response = client.exe cute (post );
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 );
}

/** Parse the returned JSON data to obtain the longitude and latitude */
Jsonobject JSON = new jsonobject (strbuff. tostring ());
Jsonobject subjosn = new jsonobject (JSON. getstring ("location "));

Longitude. Latitude = subjosn. getstring ("latitude ");
Response. longpolling = subjosn. getstring ("longpolling ");

Log. I ("longitude", longitude. Latitude + longitude. longpolling );

} Catch (exception e ){
Log. E (E. getmessage (), E. tostring ());
Throw new exception ("An error occurred while obtaining the longitude and latitude:" + E. getmessage ());
} Finally {
Post. Abort ();
Client = NULL;
}

Return response;
}

Here, the POST method is used to send JSON data to googleapi. Google returns JSON data. After the data is obtained, it is parsed to obtain latitude and longitude information.

For official instructions on Google Base Station Information API> click here to view details.

Obtain physical location

After obtaining the longitude and latitude, we convert it to a physical address.

We still use defaulthttpclient to call the Google map API to obtain physical information, but here we use the get method.

The complete method code is as follows:

/**
* Obtain the geographic location
*
* @ Throws exception
*/
Private string getlocation (swritable rows) 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 ", longitude. latitude, longitude. longpolling );
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 = new jsonobject (resultstring );
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;
}
Display result
/** Display result */
Private void showresult (scell cell, string location ){
Textview celltext = (textview) findviewbyid (R. Id. celltext );
Celltext. settext (string. Format ("base station information: MCC: % d, MNC: % d, LAC: % d, CID: % d ",
Cell. MCC, cell. MNC, cell. lac, cell. CID ));

Textview locationtext = (textview) findviewbyid (R. Id. lacationtext );
Locationtext. settext ("physical location:" + location );
}

Open the androidmanifest. xml configuration file and add the following configuration information:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

 

For the complete procedure, see simple base station positioning program.

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.