Developed in Android. It is often necessary to use the positioning function, especially for applications that rely on geolocation features. Very many people like to use the Baidu map, the great map provided by the SDK. Open API, but only need latitude and longitude, or city, street address and other information. There is no need to provide a preview map. In the application of the map interface. At this time, do not need to use Baidu map. The German map. Doing so will only add the size of the APK. What do we do?
In fact Locationmanager,geocoder these Android APIs provide us with these classes to be able to satisfy.
The following author is talking about how to use Locationmanager to obtain latitude and longitude, and the use of Geocoder to convert latitude and longitude to urban streets and other information.
Locationmanager
Locationmanager Location Manager instance obtained by Getsystemservice () method
Locationmanager = (Locationmanager) getsystemservice (Context.location_service);
get Locationmanager instance, then combine Locationprovider can get latitude and longitude, Locationprovider is divided into two kinds:
Locationprovider Gpsprovider = Locationmanager.getprovider (Locationmanager.gps_provider);//1. GPS positioning, more accurate. Also power consumption locationprovider Netprovider = Locationmanager.getprovider (Locationmanager.network_provider);//2. Navigate through the network. The positioning accuracy is not high or the situation can be considered to use
before positioning, it is necessary to infer whether the two locationprovider exist:
if (Locationmanager.getprovider (locationmanager.network_provider)! = NULL | | Locationmanager.getprovider ( Locationmanager.gps_provider) = null) { /** for positioning * PROVIDER: Locationprovider string for positioning: locationmanager.network_provider/locationmanager.gps_provider* mintime: Time update interval. Unit: Ms * mindistance: Location refresh distance, unit: m* Listener: Listener for locating updates locationlistener*/ Locationmanager.requestlocationupdates (provider, Mintime, mindistance, listener);} else { //Cannot locate: 1, prompt the user to open the location service; 2, jump to the Setup interface Toast.maketext (this, "cannot locate, please open location Service", Toast.length_short). Show (); Intent i = New Intent (); i.setaction (settings.action_location_source_settings); startactivity (i);}
When Locationprovider is not NULL, it is positioned when it is empty. Prompts the user to open the location service and jumps in the code. For users to check.
Locationlistener
When you are positioning. Need to implement a Locationlistener location listener interface, mainly overriding the Onlocaiontchanged () method
Other methods that need to be rewritten are:
@Overridepublic void Onstatuschanged (String provider, int status, Bundle extras) {//TODO auto-generated method stub} @Over ridepublic 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 to specific location information: Country, City, street name, etc.
Geocoder gc = new Geocoder (this, Locale.getdefault ()); List<address> locationlist = null;try {locationlist = gc.getfromlocation (latitude, longitude, 1);} catch ( IOException e) {e.printstacktrace ();} Address address = locationlist.get (0);//Get Address instance//log.i (TAG, "address =" + address); String countryname = Address.getcountryname ();//Get Country name, analogy: China log.i (TAG, "countryname =" + CountryName); String locality = Address.getlocality ();//Get city name, analogy: Beijing log.i (TAG, "locality =" + locality); for (int i = 0; Address.getaddressline (i)! = NULL; i++) {String addressline = Address.getaddressline (i);//Get peripheral information. Include streets and so on. I=0, get street name LOG.I (TAG, "addressline =" + Addressline);}
Finally, don't 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"/>
Correction:
Text if (Locationmanager.getprovider (locationmanager.network_provider)! = NULL | | Locationmanager.getprovider ( Locationmanager.gps_provider)! = null)
This inference should be replaced by:
if (locationmanager.isproviderenabled (locationmanager.network_provider) | | | locationmanager.isproviderenabled ( Locationmanager.gps_provider))
demo:http://download.csdn.net/detail/xiong_it/8916215.
Reprint please indicate original Xiong_it and original link: http://blog.csdn.net/xiong_it/article/details/46968477. Thank you!
Android development: Locationmanager get latitude and longitude and positioning process (with demo)