Android Base Station Location Based on geolocationapi

Source: Internet
Author: User

API address for http://code.google.com/p/gears/wiki/GeolocationAPI

Sending format:

{
"Location ":{
"Latitude": 51.0,
"Longpolling":-0.1,
"Altitude": 30.1,
"Accuracy": 1200.1,
"Altitude_accuracy": 10.1,
"Address ":{
"Street_number": "100 ",
"Street": "amphibian Gateway ",
"Postal_code": "94043 ",
"City": "Mountain View ",
"County": "Mountain View county ",
"Region": "California ",
"Country": "United States of America ",
"Country_code": "us"
}
}
}

Returned format :{
"Location ":{
"Latitude": 51.0,
"Longpolling":-0.1,
"Altitude": 30.1,
"Accuracy": 1200.1,
"Altitude_accuracy": 10.1,
"Address ":{
"Street_number": "100 ",
"Street": "amphibian Gateway ",
"Postal_code": "94043 ",
"City": "Mountain View ",
"County": "Mountain View county ",
"Region": "California ",
"Country": "United States of America ",
"Country_code": "us"
}
}
}

After obtaining the LAC and cellid, the others are parsed in JSON;

</P> <p> Import Java. io. bufferedreader; <br/> Import Java. io. inputstream; <br/> Import Java. io. inputstreamreader; <br/> Import java.net. URL; <br/> Import Java. util. date; </P> <p> Import Org. apache. HTTP. httpentity; <br/> Import Org. apache. HTTP. httpresponse; <br/> Import Org. apache. HTTP. client. methods. httppost; <br/> Import Org. apache. HTTP. entity. stringentity; <br/> Import Org. apache. HTTP. impl. client. defaulthttpc Lient; <br/> Import Org. JSON. jsonarray; <br/> Import Org. JSON. jsonobject; </P> <p> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. OS. bundle; <br/> Import android. telephony. telephonymanager; <br/> Import android. telephony. GSM. gsmcelllocation; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. textview; </P> <p> public class loca Tionstation extends activity {<br/> textview mtextview; <br/> button mbutton; <br/> telephonymanager TM; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> mtextview = (textview) findviewbyid (R. id. textview); <br/> mbutton = (button) findviewbyid (R. id. button); <br/> TM = (telephonymanager) This <br/>. getsystemservice (context. telephony_service); </P> <p> mbutton. setonclicklistener (New button. onclicklistener () {</P> <p> @ override <br/> Public void onclick (view V) {<br/> // todo auto-generated method stub <br/> gsmcelllocation GCL = (gsmcelllocation) TM. getcelllocation (); <br/> int cid = GCL. getcid (); <br/> int LAC = GCL. getlac (); <br/> system. out. prin TLN ("operator" + TM. getnetworkoperator (); // China Mobile 43600 <br/> int MCC = integer. valueof (TM. getnetworkoperator (). substring (0, <br/> 3); <br/> int MNC = integer. valueof (TM. getnetworkoperator (). substring (3, <br/> 5); <br/>/* <br/> * sending format: {<br/> "version": "1.1.0 ", <br/> "host": "maps.google.com", <br/> "access_token": "2: Authorization: 4ixoeopth1glsxe", <br/> "home_mobile_country_code": 460, <br/> "home_mo Bile_network_code ": 0, <br/>" address_language ":" zh_cn ", <br/>" radio_type ":" GSM ", <br/>" request_address ": True, <br/> "cell_towers": [<br/>{< br/> "cell_id": 36526, <br/> "location_area_code": 14556, <br/> "mobile_country_code": 460, <br/> "mobile_network_code": 0, <br/> "timing_advance ": 5555 <br/>}< br/>] <br/>}< br/> */<br/> try {<br/> // assemble a JSON query string <br /> jsonobject holder = new jsonobject (); <br/> holder. Put ("version", "1.1.0"); <br/> holder. put ("host", "maps.google.com"); <br/> holder. put ("address_language", "zh_cn"); <br/> holder. put ("request_address", true); </P> <p> jsonarray array = new jsonarray (); <br/> jsonobject DATA = new jsonobject (); <br/> data. put ("cell_id", CID); // 25070 <br/> data. put ("location_area_code", LAC); // 4474 <br/> data. put ("mobile_country_code", MCC); // 460 <br/> data. put ("Mobile _ Network_code ", MNC); // 0 <br/> array. put (data); <br/> holder. put ("cell_towers", array); </P> <p> // create a connection, send a request, and receive a response <br/> defaulthttpclient client = new defaulthttpclient (); </P> <p> httppost post = new httppost (<br/> "http://www.google.com/loc/json"); </P> <p> stringentity Se = new stringentity (holder. tostring (); </P> <p> post. setentity (SE); <br/> httpresponse resp = client.exe cute (post); </P> <p> httpentity entity = Resp. getentity (); </P> <p> bufferedreader BR = new bufferedreader (<br/> New inputstreamreader (entity. getcontent (); <br/> stringbuffer sb = new stringbuffer (); <br/> string result = BR. readline (); </P> <p> while (result! = NULL) {</P> <p> Sb. append (result); <br/> result = BR. readline (); <br/>}< br/>/* <br/> * return format: {<br/> "location ": {<br/> "latitude": 51.0, <br/> "longpolling":-0.1, <br/> "altitude": 30.1, <br/> "accuracy ": 1200.1, <br/> "altitude_accuracy": 10.1, <br/> "Address": {<br/> "street_number": "100 ", <br/> "street": "amphibian Gateway", <br/> "postal_code": "94043", <br/> "city": "Mountain View ", <br/>" County ":" Mountain View county ", <br/>" region ":" California ", <br/>" country ":" United States of America ", <br/> "country_code ": "Us" <br/>}< br/> */<br/> jsonobject = new jsonobject (sb. tostring (); </P> <p> jsonobject jsonobject1 = new jsonobject (jsonobject <br/>. getstring ("location"); </P> <p> getaddress (jsonobject1.getstring ("latitude"), jsonobject1 <br/>. getstring ("longi Tude "); </P> <p> // mtextview. settext (sb. tostring (); <br/>} catch (exception e) {<br/> // todo: handle exception <br/>}</P> <p> }); <br/>}</P> <p> void getaddress (string Lat, string lag) {<br/> try {</P> <p> URL url = new URL ("http://maps.google.cn/maps/geo? Key = abcdefg & Q = "<br/> + lat +", "+ lag); <br/> inputstream = URL. openconnection (). getinputstream (); <br/> inputstreamreader inputreader = new inputstreamreader (inputstream, <br/> "UTF-8"); <br/> bufferedreader bufreader = new bufferedreader (inputreader ); </P> <p> string line = "", lines = ""; </P> <p> while (line = bufreader. readline ())! = NULL) {<br/> lines + = line; <br/>}< br/> If (! Lines. equals ("") {</P> <p> jsonobject = new jsonobject (lines); <br/> jsonarray = new jsonarray (jsonobject. get ("placemark") <br/>. tostring (); <br/> for (INT I = 0; I <jsonarray. length (); I ++) {</P> <p> mtextview. settext (jsonarray. getjsonobject (I ). getstring ("Address"); </P> <p >}</P> <p >}catch (exception E) {<br/>;< br/>}</P> <p >}< br/>}

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.