How to get latitude and longitude in Android

Source: Internet
Author: User

A few days ago write code encountered a place need to get latitude and longitude, do not need Baidu map, because as long as the current location of the latitude and longitude on the line, the Android API has a built-in method, here to write, the following code we can take to use, here simple to, just copy into their own code, Then call the Showlocation () method to get latitude and longitude, if: Before this jingwd () method has been called, such as in OnCreate () , because this is a concrete implementation, the following is the code:

    private locationmanager locationmanager;    private  String locationProvider;    //  latitude and longitude acquisition     private  VOID&NBSP;JINGWD ()  {        //  get location Manager          locationManager =  (Locationmanager)  getsystemservice ( Context.location_service);        //  get all available location providers          list<string> providers = locationmanager.getproviders (true);        if  (Providers.contains (LocationManager.GPS_ PROVIDER)  {            //  If gps             locationProvider =  Locationmanager.gps_provider;        } else if  (Providers.contains (locationmanager.network_provider))  {             //  if it's network             locationprovider = locationmanager.network_ provider;        } else {             toast.maketext (this,  "No available location provider",  toast.length_short). Show ();            return;         }        //  Get location         Location location = locationManager                 .getlastknownlocation (LocationProvider);         if  (location != null)  {             //  not empty, show geographic latitude              showlocation (location);        }         //  Monitor Geographic Changes          Locationmanager.requestlocationupdates (locationprovider, 3000, 1,                 locationlistener);     }     /**     *  display location latitude and longitude information      *       *  @param  location     */     private void showlocation (location location)  {         jingd = location.getlongituDe ();         weid = location.getlatitude ();     }    /**     * locationlistern Listener   Parameters: Location provider, time interval for monitoring location changes, distance interval for location changes, Locationlistener listener      */     Locationlistener locationlistener = new locationlistener ()  {          @Override         public void  Onstatuschanged (STRING&NBSP;PROVIDER,&NBSP;INT&NBSP;STATUS,&NBSP;BUNDLE&NBSP;ARG2)  {         }         @Override          public void onproviderenabled (String provider)  {         }         @Override          public&nbSp;void onproviderdisabled (String provider)  {        }          @Override          Public void onlocationchanged (location location)  {             //  re-display          if location changes     showlocation (location);        }     };     @Override     protected void ondestroy ()  {        super.ondestroy ();         if  (locationmanager != null)  {             //  Remove Listeners              Locationmanager.removeupdates (LOCationlistener);         }    } 


This article is from the "Mobile Platform Development" blog, make sure to keep this source http://liuxudong1001.blog.51cto.com/10877072/1734547

How to get latitude and longitude in Android

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.