Android obtains the geographic location through the base station

Source: Internet
Author: User

/**

* Obtain Base Station Information * @ Return * @ Throws exception */ Public scell getcellinfo () throws exception { Scell cell = new scell (); Telephonymanager mtelnet = (telephonymanager) getsystemservice (context. telephony_service ); Gsmcelllocation location = (gsmcelllocation) mtelnet. getcelllocation (); If (location = NULL) Throw new exception ("the base station information is empty "); 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 (); Cell. MCC = MCC; Cell. MNC = MNC; Cell. LAC = LAC; Cell. cid = CID; Return cell; }
/** * Obtain the longitude and latitude through the base station information * @ Param Cell * @ Return * @ Throws exception */ Private situde getitude (scell cell) throws exception { S00000000= new s0000 ();
Httpclient client = new defaulthttpclient (); Httppost post = new httppost ("http://www.google.com/loc/json "); Try { 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 );
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 ); } 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 ("" + E. getmessage ()); } Finally { Post. Abort (); Client = NULL; }
Return response; }
/** * Obtain the geographical location through the latitude and longitude to Google Map * @ Param listener * @ Return * @ Throws exception */ Private string getlocation (swritable rows) throws exception { String resultstring = ""; String urlstring = string. Format ( "Http://maps.google.cn/maps/geo? Key = abcdefg & Q = % s, % s ", Longitude. latitude, longitude. longpolling ); Log. I ("url", urlstring ); Httpclient client = new defaulthttpclient (); Httpget = new httpget (urlstring ); Try { 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 (); 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 ("SD:" + E. getmessage ()); } Finally { Get. Abort (); Client = NULL; } Return resultstring; }
Public class scell { Public int MCC; Public int MNC; Public int LAC; Public int CID; }
Public class s0000 { Public String latitude; Public String longpolling; }

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.