Android GPS WiFi base station positioning

Source: Internet
Author: User
/*** Obtain the current location based on the base station */Public void getcurrentlocationagps () {telephonymanager tmanager = (telephonymanager) context. getsystemservice (context. telephony_service); If (tmanager. getcelllocation () = NULL) {return;} gsmcelllocation GCL = (gsmcelllocation) tmanager. getcelllocation (); int cid = GCL. getcid (); int LAC = GCL. getlac (); string cursor = tmanager. getnetworkoperator (); int MCC = 0; int MNC = 0; If (! Invalid. equals ("") {MCC = integer. valueof (outputs. substring (0, 3); MNC = integer. valueof (outputs. substring (3, 5);} Try {// assemble JSON query string 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); jsonarray array = new jsonarray (); jsonobject DATA = new jsonobject (); data. put ("cell_id", CID); // 25070data. put ("location_area_code", LAC); // 4474data. put ("mobile_country_code", MCC); // 460data. put ("mobile_network_code", MNC); // 0array. put (data); holder. put ("cell_towers", array); // create a connection, send a request and accept the response defaulthttpclient client = new defaulthttpclient (); httppost post = new httppost ("http://www.google.com/loc/json "); stringentity Se = new stringentity (holder. tostring (); Post. setentity (SE); httprespon Se resp = client.exe cute (post); httpentity entity = resp. getentity (); bufferedreader BR = new bufferedreader (New inputstreamreader (entity. getcontent (); stringbuffer resultstr = new stringbuffer (); string Readline = NULL; while (Readline = BR. readline ())! = NULL) {resultstr. append (Readline);} jsonobject jsonresult = new jsonobject (resultstr. tostring (); jsonobject jsonlocation = jsonresult. getjsonobject ("location"); double jsonlat = jsonlocation. getdouble ("latitude"); double jsonlon = jsonlocation. getdouble ("longpolling"); location = new location ("agps"); location. setlatitude (jsonlat); location. setlongpolling (jsonlon);} catch (exception e) {log. I ("life", "location get failed ");}}

* Obtain the current location * @ Param context */Public void getcurrentlocationwifi (context) {wifimanager = (wifimanager) Context Based on WiFi. getsystemservice (context. wifi_service); If (wifimanager. iswifienabled () {location = lmanager. getlastknownlocation (locationmanager. network_provider );}}

/*** Obtain the current location based on GPS */Public void getcurrentlocationgps () {string locationprovider = locationmanager. gps_provider; location = lmanager. getlastknownlocation (locationprovider );}
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.