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