[Java] <span style = "background-color: rgb (243,248,251); font-family: simsun;"> User positioning: </span> role of User Location: 1. get user location 2. key API1.Location Manager for tracking users' mobile User Location: used to manage the Android User Location service 2. location Providers: provides multiple positioning methods for developers to choose from. <1> GPS Provider <2> Network Provider <3> classification of Passive positioning methods: 1. GPS Positioning: GPS satellite is used for positioning, which must be in AndroidManifest. the following permissions are declared in xml: android. permission. ACCESS_FINE_LOCATION 2. NETWORK Positioning: the signal receiving tower and Wi-Fi access point are used for locating. the following permissions are declared in xml: android. permission. ACCESS_FINE_LOCATION or android. permission. ACCESS_COARSE_LOCATION the difference between the above two positioning methods is that GPS positioning accuracy is higher, but it also consumes more power to obtain the user's current location: 1. in AndroidManifest. declare the corresponding permissions in xml; 2. get the LocationManager object. 3. select Location Provider; 4. Bind The LocationListener object. There are four methods for LocationListener: 1. onLocationChanged (Location location): When the device location changes, we can call Location. getlongpolling () and location. getLatitude () to get the longitude and latitude of the device 2. onProviderDisabled (String provider): this parameter is called when the Provider of the provided data is disabled. onProviderEnabled (String provider): called when the Provider that provides data is used. onStatusChanged (String provider, int status, Bundle extras): When the status changes, we need to implement the above four methods of LocationListener: [java] <span style = "font-size: 18px; "> private class TestLocationListener implements LocationListener {@ Override public void onLocationChanged (Location location) {System. out. println (location. getlongpolling (); System. out. println (location. getLatitude () ;}@ Override public void onProviderDisabled (String provider) {// do something you need} @ Override public void onProviderEnabled (String provider) {// do something you need} @ Override public void onStatusChanged (String provider, int status, Bundle extras) {// do something you need }}</span> test the LocationProvider of the current device. Because there are more than one location method for the current device, the following method is provided for searching the location service: [java] <span style = "font-size: 18px;"> List <String> providers = locationManager. getAllProviders (); for (Iterator <String> iterator = providers. iterator (); iterator. hasNext ();) {String string = (String) iterator. next (); Log. d ("BruceZhang", string + "\ n"); </span> because there are multiple providers, you need to make a choice. Here, we provide the best Provider Selection Method: in this case, you need to use A class -- Criteria. the following is an explanation given in the Android SDK Documentation: a class indicating the application criteria for selecting A location provider. providers maybe ordered according to accuracy, power usage, ability to report altitude, speed, and bearing, and monetary cost. it provides a series of methods to set user requirements, and finally provides the best Provider required by the user. The following is the document's explanation of the setting conditions: [java] view plaincopy <span style = "font-size: 18px;"> void setAccuracy (int accuracy) Indicates the desired accuracy for latitude and longpolling. </span> [java] <span style = "font-size: 18px;"> void setAltitudeRequired (boolean altitudeRequired) Indicates whether the provider must provide altitude information. </span> [java] <span style = "font-size: 18px;"> void setBearingAccuracy (int accuracy) Indicates the desired bearing accuracy. </span> [java <span style = "font-size: 18px;"> void setBearingRequired (boolean bearingRequired) Indicates whether the provider must provide bearing information. </span> [java] <span style = "font-size: 18px;"> void setCostAllowed (boolean costAllowed) Indicates whether the provider is allowed to incur monetary cost. </span> [java] <span style = "font-size: 18px;"> void setHorizontalAccuracy (int accuracy) Indicates the desired horizontal accuracy (latitude and longness ). </span> [java] <span style = "font-size: 18px;"> void setPowerRequirement (int level) Indicates the desired maximum power level. </span> [java] <span style = "font-size: 18px;"> void setSpeedAccuracy (int accuracy) Indicates the desired speed accuracy. </span> [java] view plaincopy <span style = "font-size: 18px;"> void setSpeedRequired (boolean speedRequired) Indicates whether the provider must provide speed information. </span> [java] view plaincopy <span style = "font-size: 18px;"> void setVerticalAccuracy (int accuracy) Indicates the desired vertical accuracy (altitude ). </span> tracking user location: the methods and explanations used to update user location are as follows: [java] // public void requestLocationUpdates (String provider, // long minTime, float minDistance, LocationListener listener) // Added in API level 1 // Register for location updates using the named provider, and a pending intent. /// Parameters // provider the name of the provider with which to register // minTime minimum time interval between location updates, in milliseconds // minDistance minimum distance between location updates, in meters // listener a LocationListener whose onLocationChanged (Location) method will be called for each location update locationManager. requestLocationUpdates (LocationManager. GPS_PROVIDER, 0, 0, new TestLocationListener (); an example is provided below to implement user location, obtain the supported LocationProvider, and obtain the best Provider according to the conditions: the following is the source code: [java] public class MainActivity extends Activity {private Button button; private Button button2; private Button button3; private Button button4; private LocationManager locationManager; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button1); button2 = (Button) findViewById (R. id. button2); button3 = (Button) findViewById (R. id. button3); button4 = (Button) findViewById (R. id. button4); button. setOnClickListener (new ButtonListener (); button2.setOnClickListener (new ProviderButtonListener (); listener (new listener (); listener (new MyLocation (); locationManager = (LocationManager) MainActivity. this. getSystemService (Context. LOCATION_SERVICE);} private class ButtonListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub // LocationManager locationManager = (LocationManager) MainActivity. this. // getSystemService (Context. LOCATION_SERVICE);/** meaning of each parameter: * 1. define the currently used Location Provider * 2. minimum interval of one location update * 3. minimum distance of location update * 4. bind the listener-the method */Log will be called when the location changes. d ("BruceZhang", "Bond Success"); // public void requestLocationUpdates (String provider, // long minTime, float minDistance, LocationListener listener) // Added in API level 1 // Register for location updates using the named provider, and a pending intent. /// Parameters // provider the name of the provider with which to register // minTime minimum time interval between location updates, in milliseconds // minDistance minimum distance between location updates, in meters // listener a LocationListener whose onLocationChanged (Location) method will be called for each location update locationManager. requestLocationUpdates (LocationManager. GPS_PROVIDER, 0, 0, new TestLocationListener () ;}} private class ProviderButtonListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub List <String> providers = locationManager. getAllProviders (); for (Iterator <String> iterator = providers. iterator (); iterator. hasNext ();) {String string = (String) iterator. next (); Log. d ("BruceZhang", string + "\ n") ;}} private class BestProviderButtonListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub Criteria criteria = new Criteria (); criteria. setAccuracy (Criteria. ACCURACY_FINE); criteria. setPowerRequirement (Criteria. POWER_LOW); criteria. setAltitudeRequired (false); criteria. setSpeedRequired (false); criteria. setCostAllowed (false); // when the second parameter is set to false, You need to search whether or not the current provider is available, and set it to the optimal String provider = locationManager according to the condition. getBestProvider (criteria, false); Log. d ("BruceZhang", "The best provider is:" + provider) ;}} private class MyLocation implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub // you can specify the locationManager parameter for the user-defined service. requestLocationUpdates (LocationManager. GPS_PROVIDER, 5000,200 0, new TestLocationListener () ;}} private class TestLocationListener implements LocationListener {// The parameter of this function is the user's current Location @ Override public void onLocationChanged (Location arg0) {// TODO Auto-generated method stub // Toast. makeText (MainActivity. this, "your current longitude is:" + arg0.getlongdistance () + "," + // "your current latitude is:" + arg0.getLatitude (), // Toast. LENGTH_SHORT ). show (); Log. d ("BruceZhang", arg0.getlongpolling () + ""); Log. d ("BruceZhang", arg0.getLatitude () + "") ;}@ Override public void onProviderDisabled (String arg0) {// TODO Auto-generated method stub} @ Override public void onProviderEnabled (String arg0) {// TODO Auto-generated method stub} @ Override www.2cto.com public void onStatusChanged (String arg0, int arg1, Bundle arg2) {// TODO Auto-generated method stub }}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. activity_main, menu); return true ;}}