To complete the Android enhancement task, we mainly use the Android LocationManager object. Before performing operations on this project, we need to solve some other Android problems, permission is the first obstacle we need to remove first.
Android-enhanced LocationManager supports two types of permission requests:
1. You need to inform the system of your desired location for the user.
2. You need to tell it that you want detailed geographical information.
You need to get the location manager in the AndroidManifest. xmlxml file <manifes seems simple, but you still have to remember some things. First, we may only request the location manager in the main UI thread. We either request the LocationManager object in the onCreate call of the action, or use the LocationManager request to create an executable object running on the main thread.
As you can see, here we declare a LocationManager object, use getSystemService to get your object, and then call requestLocationUpdates. You may want to know which parameters are required when the location is updated. First, you tell the system that you want to use the location update function of the GPS device in the system. Then.
You tell it how long you want to update (in this example, the interval is 100 ms), and it is updated whenever you move more than one meter. In this way, you can quickly identify their movements and adjust their positional relationships with other objects. Finally, input an instance of the class that implements the LocationListener interface. After a request is sent for location update, the LocationListener class receives the initial location and changes the location later. Here is our LocationListener:
- LocationListener gpsListener = new LocationListener(){
- Location curLocation;
- boolean locationChanged = false;
- public void onLocationChanged(Location location)
- {
- if(curLocation == null)
- {
- curLocation = location;
- locationChanged = true;
- }
-
- if(curLocation.getLatitude() == location.getLatitude() &&
- curLocation.getLongitude() == location.getLongitude())
- locationChanged = false;
- else
- locationChanged = true;
-
- curLocation = location;
- }
- public void onProviderDisabled(String provider){}
- public void onProviderEnabled(String provider){}
- public void onStatusChanged(String provider, int status, Bundle extras){}
- };
In the code above, the onLocationChanged method is the only thing we need to care about. However, we will introduce other methods of this object. So that you can understand this object when copying it to your own code. Once the satellite locks the device, the method onLocationChanged will be called, and each time after the specified interval (100 ms in this example) in request updating, it will be called once.
Every time the Location is updated, the Android enhancement function will bring a Location object. Through this class, we can obtain the longitude and latitude of the target and accomplish many important tasks. Here we are most interested in getLatitude (), getlongdistance (), bearingTo () and distanceTo (). Using these four functions, we can calculate the azimuth of any subsequent positions and determine the distance from you.