in the Android development, often need to use the positioning function, especially rely on the location function of the application, many people like to use Baidu Map, the great map provided by the SDK, open API, but in only need latitude and longitude, or city, street address and other information, do not need to provide a preview map, The application of the map interface, at this time, do not need to use Baidu Map, the German map, this will only increase the size of the APK, how to do?
In fact Locationmanager,geocoder these Android APIs to provide us with these classes can be satisfied. 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 instances are obtained by Getsystemservice () method
Locationmanager = (Locationmanager) getsystemservice (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 compare power consumption locationprovider Netprovider = Locationmanager.getprovider (Locationmanager.network_provider);//2. Through Network positioning, The positioning accuracy is not high or the situation can be considered to use
before positioning, it is necessary to determine 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 * Mintime: Time update interval, unit: MS * mindistance: Location refresh distance, unit: m* Listener: Listener for location update 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, and when it is empty, prompts the user to open the location service and jumps in the code for the user to tick.
Locationlistener
When locating, it is necessary to implement a Locationlistener position listening interface, mainly rewriting 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 can be used to convert latitude and longitude to detailed location information
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, for example: China log.i (TAG, "countryname =" + CountryName); String locality = Address.getlocality ();//Get city name, for example: Beijing log.i (TAG, "locality =" + locality); for (int i = 0; Address.getaddressline (i)! = NULL; i++) {String addressline = Address.getaddressline (i);//get surrounding information, including street, 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"/>
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
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android development: Locationmanager get latitude and longitude and positioning process (with demo)