Android development: LocationManager's process of obtaining longitude and latitude and positioning (with demo ),
Location functions are often used in Android development, especially those that depend on the location function. Many people prefer Baidu map and AMAP sdks and open APIs, however, if you only need information such as longitude and latitude, city, street address, and other information, you do not need to provide a preview map. In the application of the map interface, you do not need to use Baidu map or AMAP, this will only increase the size of the apk. What should I do?
In fact, the Android APIs LocationManager and Geocoder provide us with these classes. Next I will talk about how to use LocationManager to obtain the latitude and longitude, and use Geocoder to convert the latitude and longitude to the city streets.
LocationManager
LocationManager locates the manager instance and obtains it through getSystemService ().
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
After obtaining the LocationManager instance, combine the LocationProvider to obtain the longitude and latitude. There are two types of LocationProvider:
LocationProvider gpsProvider = locationManager. getProvider (LocationManager. GPS_PROVIDER); // 1. GPS positioning is more accurate, and the power consumption of LocationProvider netProvider = locationManager is also compared. getProvider (LocationManager. NETWORK_PROVIDER); // 2. using Network Positioning, you can consider low positioning accuracy or low point saving.
Before locating, You need to determine whether two locationproviders exist:
If (locationManager. getProvider (LocationManager. NETWORK_PROVIDER )! = Null | locationManager. getProvider (LocationManager. GPS_PROVIDER )! = Null) {/** locate * provider: locationProvider string used for locating: LocationManager. NETWORK_PROVIDER/LocationManager. GPS_PROVIDER * minTime: time update interval. Unit: ms * minDistance: Location refresh distance. Unit: m * listener: used to locate the updated listener locationListener */locationManager. requestLocationUpdates (provider, minTime, minDistance, listener);} else {// cannot be located: 1. prompt the user to open the locating service; 2. Jump to the setting interface Toast. makeText (this, "unable to locate, please open the location service", Toast. LENGTH_SHORT ). show (); Intent I = new Intent (); I. setAction (Settings. ACTION_LOCATION_SOURCE_SETTINGS); startActivity (I );}When the LocationProvider is not null, locate it. When the LocationProvider is null, the system prompts the user to open the locating service and jumps to the Service in the Code for selection.
LocationListener
When locating, You need to implement a LocationListener location listening interface, which is mainly to override the onLocaiontChanged () method.
/*** Callback Method for location change * @ param Location current location * @ return void */public void onLocationChanged (location) {// obtain the latitude double latitude = Location. getLatitude (); // obtain the longitude double longdistance = location. getlongpolling ();}
Other methods to be rewritten include:
@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}
Geocoder
Geocoder latitude and longitude decoder can be used to convert latitude and longitude into detailed location information: country, city, street name, etc.
Geocoder gc = new Geocoder (this, Locale. getDefault (); List <Address> locationList = null; try {locationList = gc. getFromLocation (latitude, longpolling, 1);} catch (IOException e) {e. printStackTrace ();} Address address = locationList. get (0); // get Address instance // Log. I (TAG, "address =" + address); String countryName = address. getCountryName (); // obtain the country name, for example, China Log. I (TAG, "countryName =" + countryName); String localit Y = address. getLocality (); // obtain the city name, for example, Beijing Log. I (TAG, "locality =" + locality); for (int I = 0; address. getAddressLine (I )! = Null; I ++) {String addressLine = address. getAddressLine (I); // obtain the surrounding information, including the sub-district, I = 0, and obtain the sub-district Name Log. I (TAG, "addressLine =" + addressLine );}
Finally, do not forget to add permissions:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" />
Demo: http://download.csdn.net/detail/xiong_it/8916215.
For more information, see http://blog.csdn.net/xiong_it/article/details/46968477,thank you!
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.