Three positioning methods for android and three positioning methods for android
Android positioning Methods
I recently read about android positioning methods, checked a lot of information, conducted relevant experiments, and conducted tests on mobile phones. The following is a summary:
There are three positioning Methods: GPS, network, and base station. However, no matter which method, you must enable the network or GPS
First add permissions
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
COARSE_LOCATION is used when the base station is located. If you do not have this permission, an error is returned when getCellLocation is obtained.
The first method is implemented by JASON, which is based on the base station. The reference address is http://www.cnblogs.com/dartagnan/archive/2011/3/9.html. the download is only the code of the current location.
/*** Implementation of Google positioning. <br/> * For more information about Geolocation, see <br/> * <a * href ="Http://code.google.com/apis/gears/geolocation_network_protocol.html"Mce_href ="Http://code.google.com/apis/gears/geolocation_network_protocol.html"> *Http://code.google.com/apis/gears/geolocation_network_protocol.html</A> */public class LocationAct extends Activity {private TextView txtInfo; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Button btn = (Button) findViewById (R. id. btnStart); txtInfo = (TextView) findViewById(R.id.txt Info); btn. setOnClickListener (new Button. onClickListener () {public void onClick (View view) {getLocation ();}}) ;} Private void getLocation () {TelephonyManager tm = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE); GsmCellLocation gsmCell = (GsmCellLocation) tm. getCellLocation (); int cid = gsmCell. getCid (); int lac = gsmCell. getLac (); String netOperator = tm. getNetworkOperator (); int mcc = Integer. valueOf (netOperator. substring (0, 3); int mnc = Integer. valueOf (netOperator. substring (3, 5); J SONObject holder = new JSONObject (); JSONArray array = new JSONArray (); JSONObject data = new JSONObject (); try {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"); data. put ("cell_id", cid); data. put ("location_area_code", lac); data. put ("m Obile_countyr_code ", mcc); data. put ("mobile_network_code", mnc); array. put (data); holder. put ("cell_towers", array);} catch (JSONException e) {e. printStackTrace ();} DefaultHttpClient client = new DefaultHttpClient (); HttpPost httpPost = new HttpPost ("http://www.google.com/loc/json"); StringEntity stringEntity = null; try {stringEntity = new StringEntity (holder. toString ();} catch (UnsupportedE NcodingException e) {e. printStackTrace ();} httpPost. setEntity (stringEntity); HttpResponse httpResponse = null; try {httpResponse = client.exe cute (httpPost);} catch (ClientProtocolException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} HttpEntity httpEntity = httpResponse. getEntity (); InputStream is = null; try {is = httpEntity. getContent ();} catch (IllegalStateEx Ception e) {e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} InputStreamReader isr = new InputStreamReader (is); BufferedReader reader = new BufferedReader (isr); StringBuffer stringBuffer = new StringBuffer (); try {String result = ""; while (result = reader. readLine ())! = Null) {stringBuffer. append (result) ;}} catch (IOException e) {e. printStackTrace () ;}txtinfo. setText (stringBuffer. toString ());}}
Second, use a strict GPS to locate the location.
Public class MainActivity extends Activity {private LocationManager locationManager; private GpsStatus gpsstatus; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // get the LocationManager object locationManager = (LocationManager) getSystemService (LOCATION_SERVICE); // obtain the most compliant provider object String currentProvider = l based on the configured Criteria object OcationManager. getProvider (LocationManager. GPS_PROVIDER ). getName (); // obtain the last Location information based on the current provider Object Location currentLocation = locationManager. getLastKnownLocation (currentProvider); // if the location information is null, request to update the location information if (currentLocation = null) {locationManager. requestLocationUpdates (currentProvider, 0, 0, locationListener);} // adds the GPS status listener locationManager. addGpsStatusListener (gpsListener); // until the last location information is obtained, if the most The last location information is displayed. The default latitude and longitude // obtains the location information every 10 seconds. while (true) {currentLocation = locationManager. getLastKnownLocation (currentProvider); if (currentLocation! = Null) {Log. d ("Location", "Latitude:" + currentLocation. getLatitude (); Log. d ("Location", "location:" + currentLocation. getlongpolling (); break;} else {Log. d ("Location", "Latitude:" + 0); Log. d ("Location", "location:" + 0);} try {Thread. sleep (10000);} catch (InterruptedException e) {Log. e ("Location", e. getMessage () ;}} private GpsStatus. listener gpsListener = new GpsStatus. listener () {// triggered when the GPS status changes @ Override public void onGpsStatusChanged (int event) {// retrieves the current gpsstatus = locationManager. getGpsStatus (null); switch (event) {// event case GpsStatus when the first position is located. GPS_EVENT_FIRST_FIX: break; // start to locate the event case GpsStatus. GPS_EVENT_STARTED: break; // send the GPS satellite status event case GpsStatus. GPS_EVENT_SATELLITE_STATUS: Toast. makeText (MainActivity. this, "GPS_EVENT_SATELLITE_STATUS", Toast. LENGTH_SHORT ). show (); Iterable <GpsSatellite> allSatellites = gpsstatus. getSatellites (); Iterator <GpsSatellite> it = allSatellites. iterator (); int count = 0; while (it. hasNext () {count ++;} Toast. makeText (MainActivity. this, "Satellite Count:" + count, Toast. LENGTH_SHORT ). show (); break; // stop locating event case GpsStatus. GPS_EVENT_STOPPED: Log. d ("Location", "GPS_EVENT_STOPPED"); break ;}}; // create a Location listener private LocationListener locationListener = new LocationListener () {// call @ Override public void onLocationChanged (Location location) {Log when the Location is changed. d ("Location", "onLocationChanged");} // call @ Override public void onProviderDisabled (String provider) {Log. d ("Location", "onProviderDisabled");} // call @ Override public void onProviderEnabled (String provider) {Log when the provider is enabled. d ("Location", "onProviderEnabled");} // call @ Override public void onStatusChanged (String provider, int status, Bundle extras) {Log when the status changes. d ("Location", "onStatusChanged ");}};}
The third method is to locate the problem through the network.
Package com. test; import java. io. IOException; import java. util. list; import android. app. activity; import android. location. address; import android. location. criteria; import android. location. geocoder; import android. location. location; import android. location. locationListener; import android. location. locationManager; import android. OS. bundle; import android. util. log; import android. widget. toast; publi C class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // get the LocationManager object LocationManager locationManager = (LocationManager) getSystemService (LOCATION_SERVICE); // create a Criteria object Criteria criteria = new Criteria (); // sets the rough precision of criteria. setAccuracy (Criteria. ACCURACY_COARSE); // sets whether to return to the sea. Unplug information criteria. setAltitudeRequired (false); // sets whether to return the location information criteria. setBearingRequired (false); // sets whether the paid service criteria is allowed. setCostAllowed (true); // sets the power consumption level criteria. setPowerRequirement (Criteria. POWER_HIGH); // sets whether to return the speed information criteria. setSpeedRequired (false); // obtain the most compliant provider object String currentProvider = locationManager Based on the configured Criteria object. getBestProvider (criteria, true); Log. d ("Location", "currentProvider:" + c UrrentProvider); // obtain the last Location information based on the current provider Object Location currentLocation = locationManager. getLastKnownLocation (currentProvider); // if the location information is null, request to update the location information if (currentLocation = null) {locationManager. requestLocationUpdates (currentProvider, 0, 0, locationListener);} // until the last location information is obtained. If the last location information is not obtained, the default longitude and latitude are displayed. // obtain the location information once every 10 seconds while (true) {currentLocation = locationManager. getLastKnownLocation (currentProvide R); if (currentLocation! = Null) {Log. d ("Location", "Latitude:" + currentLocation. getLatitude (); Log. d ("Location", "location:" + currentLocation. getlongpolling (); break;} else {Log. d ("Location", "Latitude:" + 0); Log. d ("Location", "location:" + 0);} try {Thread. sleep (10000);} catch (InterruptedException e) {Log. e ("Location", e. getMessage () ;}// parse the address and display Geocoder geoCoder = new Geocoder (this); try {int latitude = (int) currentLocation. getLatitude (); int longpolling = (int) currentLocation. getlongpolling (); List <Address> list = geoCoder. getFromLocation (latitude, longpolling, 2); for (int I = 0; I <list. size (); I ++) {Address address = list. get (I); Toast. makeText (MainActivity. this, address. getCountryName () + address. getAdminArea () + address. getFeatureName (), Toast. LENGTH_LONG ). show () ;}} catch (IOException e) {Toast. makeText (MainActivity. this, e. getMessage (), Toast. LENGTH_LONG ). show () ;}// create a Location listener private LocationListener locationListener = new LocationListener () {// call @ Override public void onLocationChanged (location Location) {Log when the location listener is changed. d ("Location", "onLocationChanged"); Log. d ("Location", "onLocationChanged Latitude" + location. getLatitude (); Log. d ("Location", "onLocationChanged location" + location. getlongpolling ();} // call @ Override public void onProviderDisabled (String provider) {Log when the provider fails. d ("Location", "onProviderDisabled");} // call @ Override public void onProviderEnabled (String provider) {Log when the provider is enabled. d ("Location", "onProviderEnabled");} // call @ Override public void onStatusChanged (String provider, int status, Bundle extras) {Log when the status changes. d ("Location", "onStatusChanged ");}};}