App may encounter a demand, is the current location of the coordinates to the server, today I provide three ways to obtain latitude and longitude coordinates information, the first is through the Android API to achieve, the second through the Baidu Map API to achieve, the third through the world map API to achieve.
The first method (Android API implementation), nonsense not much to say, on the code.
The mainactivity code is as follows:
public class Mainactivity extends Activity {private static final String TAG = MainActivity.class.getSimpleName ();p rivate D Ouble latitude = 0.0;private double longitude = 0.0;private TextView info;private locationmanager Locationmanager; @Overri deprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); info = (TextView) Findviewbyid (r.id.tv); Locationmanager = (Locationmanager) getsystemservice ( Context.location_service); if (locationmanager.isproviderenabled (Locationmanager.gps_provider)) {getLocation ();// GPS is turned on} else {Togglegps (); new Handler () {}.postdelayed (new Runnable () {@Overridepublic void run () {getLocation ()}, 2000);}} private void Togglegps () {Intent gpsintent = new Intent (); Gpsintent.setclassname ("Com.android.settings", " Com.android.settings.widget.SettingsAppWidgetProvider "); Gpsintent.addcategory (" Android.intent.category.ALTERNATIVE "); Gpsintent.setdata (Uri.parse (" Custom:3 ")); try {pendingintent.getbroadcast ( This, 0, GPSIntent, 0). Send ();} catch (Canceledexception e) {e.printstacktrace (); Locationmanager.requestlocationupdates (Locationmanager.network_ PROVIDER, 0, Locationlistener); Location Location1 = locationmanager.getlastknownlocation (Locationmanager.network_provider); if (location1! = null) { Latitude = Location1.getlatitude (); Longitude longitude = location1.getlongitude (); Latitude}}}private void GetLocation () {Location location = locationmanager.getlastknownlocation (locationmanager.gps_ PROVIDER); if (location = null) {latitude = Location.getlatitude (); longitude = Location.getlongitude ();} else { Locationmanager.requestlocationupdates (locationmanager.gps_provider, 0, locationlistener);} Info.settext ("Latitude:" + latitude + "\ n" + "Longitude:" + longitude);} Locationlistener Locationlistener = new Locationlistener () the state of the {//Provider trigger this function when it is available, temporarily unavailable, and without service three states are switched directly @overridepublic void Onstatuschanged (String provider, int status, Bundle extras) {}//Provider is triggered when the function is enable, such as GPS is turned on @overridepublic void Onproviderenabled (String provider) {LOG.E (TAG, provider);} This function is triggered when provider is disable, such as when the GPS is closed @overridepublic void onproviderdisabled (String provider) {LOG.E (TAG, provider);} This function is triggered when coordinates change, and if provider is passed into the same coordinates, it will not be triggered @overridepublic void onlocationchanged (location location) {if (Location! = NULL) {LOG.E ("Map", "Location Changed:lat:" + location.getlatitude () + "Lng:" + location.getlongitude ()); latitude = l Ocation.getlatitude (); Longitude longitude = location.getlongitude (); Latitude}}};/* * * Open and Close GPS the second method * private void Opengpssettings () {//Get GPS current status (on or off status) Boolean gpsenabled = Settings.secur E.islocationproviderenabled (Getcontentresolver (), locationmanager.gps_provider); if (gpsenabled) {// Close GPSSettings.Secure.setLocationProviderEnabled (Getcontentresolver (), Locationmanager.gps_provider, false);} else {//Turn on GPS Settings.Secure.setLocationProviderEnabled (Getcontentresolver (), Locationmanager.gps_provider, True) ;}} */}The Main.xml layout is as follows
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:id=" @+id/layout " android:layout_width=" Match_parent " android: layout_height= "Match_parent" android:background= "@android: Color/white" android:orientation= "vertical" > <textview android:id= "@+id/tv" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:text= "latitude and longitude information:" android:textcolor= "#660000" android:textsize= "20SP" /></linearlayout>
The manifest file is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.tqmapdemo "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>" <!--Internet Internet access---<uses -permission android:name= "Android.permission.INTERNET"/> <!--GPS positioning rights--<uses-permission Android:name = "Android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name= "android.permission.ACCESS_ Fine_location "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_launcher " Android:label= "@string/app_name" Android:theme= "@android: Style/theme.black" > <activity Android:name= "com.example.tqmapdemo.MainActivity" android:label= "@string/app_name" > <intent-f Ilter> <action Android:namE= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application></manifest>
The operation results are as follows
Download Demo please poke
The second method (Baidu Map API implementation, Note: You need to apply for Apikey)
Download Demo please poke
Third Method (Heaven and Earth Diagram API implementation)
Download Demo please poke
Android GPS get current latitude and longitude coordinates