Topic: [original] simple use of LocationManager in Android to get the current location

Source: Internet
Author: User

In Android, LocationManager provides a series of methods for location-related problems, including querying the previous known location; registering/deregistering periodic location updates from a LocationProvider; and trigger a defined Intent when the registration/logout approaches a coordinate. Today, let's take a look at the simple use of LocatinManager in Android, taking obtaining the current location as an example.

First, we need to obtain an instance of the LocationManager. Here, we need to note that its instance can only be obtained in the following way. Directly instantiating the LocationManager is not allowed.

Java code
LocationManager locationManager = (LocationManager) getSystemService (Context. LOCATION_SERVICE );

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

After obtaining the LocationManager instance locatonManager, we use the following statement to register a periodic location update.
Java code
LocationManager. requestLocationUpdates (LocationManager. GPS_PROVIDER,
1000, 0, locationListener );

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);

This code tells the system that we need to obtain location information from GPS and update the information every MS without considering location changes. The last parameter is a reference of LocationListener. We must implement this class.

Java code
Private final LocationListener locationListener = new LocationListener (){
Public void onLocationChanged (Location location) {// This function is triggered when the coordinates change. If the Provider transmits the same coordinates, it will not be triggered.
// Log it when the location changes
If (location! = Null ){
Log. I ("SuperMap", "Location changed: Lat :"
+ Location. getLatitude () + "Lng :"
+ Location. getlongpolling ());
}
}

Public void onProviderDisabled (String provider ){
// This function is triggered when the Provider is disable, for example, GPS is disabled.
}

Public void onProviderEnabled (String provider ){
// This function is triggered when the Provider is enabled. For example, if GPS is enabled
}

Public void onStatusChanged (String provider, int status, Bundle extras ){
// Trigger this function when the Provider's transition state is available, temporarily unavailable, and no service is directly switched
}
};

Private final LocationListener locationListener = new LocationListener () {public void onLocationChanged (Location location) {// This function is triggered when the coordinates change. If the Provider transmits the same coordinates, it will not be triggered // log it when the location changes if (location! = Null) {Log. I ("SuperMap", "Location changed: Lat:" + location. getLatitude () + "Lng:" + location. getlongpolling () ;}} public void onProviderDisabled (String provider) {// This function is triggered when the Provider is disable, for example, GPS is disabled} public void onProviderEnabled (String provider) {// This function is triggered when the Provider is enabled, for example, GPS is enabled} public void onStatusChanged (String provider, int status, Bundle extras) {// trigger this function when the Provider's transition state is available, temporarily unavailable, and no service is directly switched }};

The above steps should generally be completed in the onCreate () stage of the Activity.
After successfully registering a periodic coordinate update, we can use the following method to obtain the current coordinate at any time.
Java code
Location = locationmanager. getlastknownlocation (locationmanager. gps_provider );
Double latitude = location. getlatitude (); // longitude
Double longpolling = location. getlongpolling (); // latitude
Double altitude = location. getaltitude (); // altitude

Location = locationmanager. getlastknownlocation (locationmanager. gps_provider); double latitude = location. getlatitude (); // longitude double longdistance = location. getlongpolling (); // latitude double altitude = location. getaltitude (); // altitude

However, if you try to run the locationsample program at this time, most errors will be reported when the program starts, because we do not have permission to set GPS, and the solution is quite simple, in androidmanifest. add the following statement to the block in XML to solve the permission problem. For detailed permission settings, see the official documentation docs/reference/Android/manifest.permission.html

Java code
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

If debugging is in the simulator, we have two methods to set a simulated coordinate value. The first method is to use DDMS. We can use this method in the ADT plug-in of eclipse, you only need to open "Window" -- & gt; "Show View" and open "Emulator Control" View to see the following settings Window. We can manually, alternatively, you can use the KML and GPX files to set a coordinate.


Another method is to use the geo command. We need to telnet to port 5554 of the local machine, and then enter a command similar to the geo fix-121.45356 46.51119 4392 in the command line, the following three parameters represent the longitude, latitude, and (optional) altitude.

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.