Original Simple use of Locationmanager in Android to get the current location

Source: Internet
Author: User

Android Locationmanager provides a range of approaches to geolocation-related issues, including querying a known location, registering/unregistering periodic location updates from a locationprovider, and registering/ Unregisters a defined intent when close to a certain coordinate. Today we'll take a look at the simple use of Locatinmanager in Android to get the current location as an example.

First, we need to get an instance of Locationmanager, it is important to note that his instance can only be obtained by the following way, the direct instantiation of Locationmanager is not allowed.

Java code
    1. Locationmanager Locationmanager = (locationmanager) getsystemservice (Context.location_service);



After getting the instance of Locationmanager Locatonmanager, we register a periodic location update with the following statement.

Java code
    1. Locationmanager.requestlocationupdates (Locationmanager.gps_provider,
    2. 0, Locationlistener);



This code tells the system that we need to get the location information from the GPS and is updated every 1000ms, regardless of the location change. The last parameter is a reference to Locationlistener, and we have to implement this class.

Java code
  1. Private final Locationlistener Locationlistener = new Locationlistener () {
  2. public void onlocationchanged (position location) { //when coordinates change, this function is triggered, and if provider is passed in the same coordinates, it will not be triggered
  3. //Log It when the location changes
  4. if (location! = null) {
  5. LOG.I ("Supermap", "location Changed:lat:"
  6. + location.getlatitude () + "Lng:"
  7. + Location.getlongitude ());
  8. }
  9. }
  10. public void onproviderdisabled (String provider) {
  11. This function is triggered when the provider is disable, such as when the GPS is closed
  12. }
  13. public void onproviderenabled (String provider) {
  14. ///Provider triggers this function when enable, e.g. GPS is turned on
  15. }
  16. public void Onstatuschanged (String provider, int status, Bundle extras) {
  17. ///provider This function is triggered when a switch is available, temporarily unavailable, and no service in three states
  18. }
  19. };



These steps should generally be done in the oncreate () phase of the activity.

After successfully registering a periodic coordinate update, we can get the current coordinates at any time by the following method.

Java code
    1. Location location = locationmanager.getlastknownlocation (Locationmanager.gps_provider);
    2. Double latitude = location.getlatitude (); //Longitude
    3. Double longitude = location.getlongitude (); //Latitude
    4. Double altitude = location.getaltitude (); //Altitude



But at this time, if you try to run this locationsample, the program will probably start the error, because we do not set the GPS-related permissions, the solution is quite simple, Add the following sentence to the block in Androidmanifest.xml to resolve the permissions issue. For detailed permission settings, please refer to the official documentation docs/reference/android/manifest.permission.html

Java code
    1. <uses-permission android:name="Android.permission.ACCESS_FINE_LOCATION"/>



If you are debugging in the simulator, we have two methods to set up a simulated coordinate value, the first one is through DDMS, we can use this method in Eclipse's ADT plugin, just open "window"--&gt; "Show View" opens "Emulator Control" view to see the following Settings window, we can manually, or through the KML and GPX file to set a coordinate.

Another way is to use the GEO command, we need to telnet to the 5554 port of this machine, and then enter the command line similar to the Geo fix-121.45356 46.51119 4392, the following three parameters represent the longitude, latitude and (optional) elevation.

Original Simple use of Locationmanager in Android to get the current location

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.