Android development: LocationManager's process of obtaining longitude and latitude and positioning (with demo)

Source: Internet
Author: User

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

The LocationManager instance is obtained by getSystemService ().

 

locationManager = (LocationManager) getSystemService(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 * minTime: time update interval, unit: ms * minDistance: Location refresh distance, unit: m * listener: 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, cannot be located. 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 can be used to convert longitude and latitude to detailed location information

 

Geocoder gc = new Geocoder (this, Locale. getDefault (); List 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 locality = address. ge TLocality (); // 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:

 

    
     
      
     
   
  
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.