This article shares the specific code for Android to obtain location information for your reference. The specific content is as follows
1. Introduction to location services: Location service, translated in English as Location-Based Services, abbreviated as LBS, also known as location services or location-based services. It integrates GPS positioning, mobile communication, navigation and other technologies to provide comprehensive application services related to spatial location. Location-based services are developing rapidly, involving all aspects of business, medical care, work and life, and provide users with a series of services such as positioning, tracking and sensitive area warnings. For example, Google Maps and Baidu Maps all require location services.
The platform supports the API that provides location services. During the development process, LocationManager and LocationProviders objects are mainly used:
(1).LocationManager can be used to obtain the current location, track the device's movement route, or set a sensitive area. The device will issue a specific alarm when entering or leaving the sensitive area.
(2).LocationProviders is a collection of components that provide positioning functions. Each component in the set provides the current location of the device with different technologies, and the difference lies in the accuracy, speed and cost of positioning.
3. Next, we will describe an example of how to obtain the latitude and longitude of the position, and if the position changes, how to change the latitude and longitude of the position. Here we take the LocationManager object as an example:
(1). First, the first step is to obtain the LocationManager object, which can be obtained by calling the() function. The code is as follows:
String serviceString = Context.LOCATION_SERVICE;// What you get is location serviceLocationManager locationManager = (LocationManager) getSystemService(serviceString);// CallgetSystemService()Method to obtainLocationManagerObject
Among them, LOCATION_SERVICE is an update of system-level services, control locations and other devices supported by Android.
(2). After obtaining the LocationManager object, you also need to specify the LocationManager location method, and then you can call the () method to obtain the current location. Currently, there are two main location methods for LocationManager.
GPS positioning: It can provide more precise location information, but the positioning speed and quality are affected by the number of satellites and environmental conditions, and requires .ACCESS_FINE_LOCATION user permission.
Network positioning: The location information provided has poor accuracy, but the speed is faster than GPS positioning. Using base stations or WiFi access to provide approximate location information, you need to have the following permissions: .ACCESS_COARSE_LOCATION or .ACCESS_FINE_LOCATION.
Note: (The static constants of the LocationManager class that uses GPS positioning and network positioning are different. The static constants of the LocationManager class that uses GPS positioning are: GPS_PROVIDER, and the static constants of the LocationManager class that is located in the network positioning are: NETWORK_PROVIDER. These two static constants are used when obtaining the current location.)
Take GPS positioning as an example and obtain the location information code as follows:
String provider = LocationManager.GPS_PROVIDER;// Specify the location method of LocationManagerLocation location = (provider);// CallgetLastKnownLocation()Method to obtain current location information
(3). By calling the getLatitude() and getLonggitude() methods in Location, you can obtain the latitude and longitude in the location information respectively. The code is as follows:
double lat = ();//Get latitudedouble lng = ();//Obtain longitude
(4). In many applications that provide positioning services, they not only need to obtain the current location information, but also need to monitor the changes in locations. A specific processing method is called when the location changes. The LocationManager provides a convenient and efficient location monitoring method requestLocationUpdates(), which can be set according to the location distance change and time interval to generate the conditions for position change events. This can avoid a large number of position change events due to slight distance changes. The code for monitoring the location changes in LocationManager is as follows:
(provider, 2000, 10,locationListener); // The condition for generating the position change event is set to change distance by 10 meters, the time interval is 2 seconds, and the monitoring position change is set
Next, we introduce the various parameters of the above line of code. The first parameter is the location method we specified before, GPS positioning or network positioning. The second parameter refers to the time interval for generating position change events, in microseconds, the third parameter refers to the distance condition, in meters, and the fourth parameter is a callback function, which is used to handle position change events, that is, to set the LocationListener listener. In general, that line of code sets the condition that generates the position change event to change 10 meters and the time interval is 2 seconds.
(5). The code to implement the locationListener is as follows:
private final LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String arg0) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } };
Next, we will briefly introduce the four methods under the above code to implement LocationListener:
The method onLocationChanged() is called when the position changes, the method onProviderDisabled() is called when the user disables the hardware with positioning function, the method onProviderEnabled() is called when the user enables the hardware with positioning function, and the method onStatusChanged() is called when the hardware state of the positioning function changes, for example, from the state where the positioning information cannot be obtained to the state where the positioning information can be obtained, and vice versa.
(6). In order to make the GPS positioning function take effect, you also need to add user permissions to the file, that is, add the following line of code and add user permissions:
<uses-permission android:name=".ACCESS_FINE_LOCATION"></uses-permission>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.