There may be a need in the app,Is to pass the coordinates of the current location to the server,Today I provide three ways to get the latitude and longitude coordinate information.The first is realized through the android api, the second is realized through the Baidu map api, and the third is realized through the sky map api.

The first method (implemented by the android api), not to say nonsense,On the code.

The mainactivity code is as follows:

public class mainactivity extends activity {
 private static final string tag=mainactivity.class.getsimplename ();
 private double latitude=0.0;
 private double longitude=0.0;
 private textview info;
 private locationmanager locationmanager;
 protected 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 () {
 public 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, 1000, 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, 1000, 0, locationlistener);
 info.settext ("Latitude:" + latitude + "\ n" + "Longitude:" + longitude);
 locationlistener locationlistener=new locationlistener () {
 //The status of the provider is triggered when the three states of available, temporarily unavailable, and no service are directly switched
 public void onstatuschanged (string provider, int status, bundle extras) {
 //This function is triggered when the provider is enabled,Such as gps is turned on
 public void onproviderenabled (string provider) {
 log.e (tag, provider);
 //This function is triggered when the provider is disabled,Such as gps is turned off
 public void onproviderdisabled (string provider) {
 log.e (tag, provider);
 //This function is triggered when the coordinates change,If the provider passes in the same coordinates,It will not be triggered
 public void onlocationchanged (location location) {
 if (location!=null) {
 log.e ("map", "location changed:lat:" + location.getlatitude () + "lng:" + location.getlongitude ());
 latitude=location.getlatitude ();//longitude
 longitude=location.getlongitude ();//latitude
 * Turn GPS on and off
 * private void opengpssettings () {
 //Get the current status of the GPS (on or off)
 boolean gpsenabled=settings.secure.islocationproviderenabled (getcontentresolver (), locationmanager.gps_provider);
 if (gpsenabled) {
 //close gps
 settings.secure.setlocationproviderenabled (getcontentresolver (), locationmanager.gps_provider, false);
 } else {
 //open gps
 settings.secure.setlocationproviderenabled (getcontentresolver (), locationmanager.gps_provider, true);
 } * /

main.xml layout is as follows

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@ + id/layout"
 android:background="@ android:color/white"
  android:id="@ + id/tv"
  android:text="latitude and longitude information:"
  android:textsize="20sp" />

The manifest file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  android:targetsdkversion="18" />
 <!-Internet permission to connect to the internet->
 <uses-permission android:name="android.permission.internet" />
 <!-Gps positioning permissions->
 <uses-permission android:name="android.permission.access_coarse_location" />
 <uses-permission android:name="android.permission.access_fine_location" />
  android:icon="@ drawable/ic_launcher"
  android:label="@ string/app_name"
  android:theme="@ android:style/theme.black">
   android:label="@ string/app_name">
    <action android:name="android.intent.action.main" />
    <category android:name="android.intent.category.launcher" />

The results are as follows

The second method(Baidu map api implementation, note:you need to apply for apikey yourself)

The third method(Sky Map API implementation)

  • Previous Android achieves beautiful Gallery gallery
  • Next 30 minutes to build an API server with Nodejs