Android GPS obtains the coordinates of the current longitude and latitude

Source: Internet
Author: User

Android GPS obtains the coordinates of the current longitude and latitude

In the APP, there may be a need to upload the coordinates of the current location to the server. Today, I provide three ways to obtain the coordinates of the longitude and latitude. The first is implemented through the Android API, the second is implemented through the Baidu map API, and the third is implemented through the map world API.

The first method (Android API implementation) is not much nonsense.

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 longpolling = 0.0; private TextView info; private LocationManager locationManager; @ Overrideprotected 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 enabled} 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, 1000, 0, locationListener); Location location1 = locationMan Ager. getLastKnownLocation (LocationManager. NETWORK_PROVIDER); if (location1! = Null) {latitude = location1.getLatitude (); // longitude longpolling = location1.getlongpolling (); // latitude }}private void getLocation () {Location location = locationManager. getLastKnownLocation (LocationManager. GPS_PROVIDER); if (location! = Null) {latitude = location. getLatitude (); longdistance = location. getlongpolling ();} else {locationManager. requestLocationUpdates (LocationManager. GPS_PROVIDER, 1000, 0, locationListener);} info. setText ("latitude:" + latitude + "\ n" + "longitude:" + longpolling);} LocationListener locationListener = new LocationListener () {// trigger this function @ Overridepublic void onStatusChanged (String Provider, int Status, Bundle extras) {} // This function is triggered when the Provider is enabled. For example, if GPS is enabled @ Overridepublic void onProviderEnabled (String provider) {Log. e (TAG, provider);} // This function is triggered when the Provider is disable. For example, if GPS is disabled @ Overridepublic void onProviderDisabled (String provider) {Log. e (TAG, provider) ;}// this function is triggered when the coordinates change. If the Provider transmits 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. getlongpolling (); latitude = location. getLatitude (); // longitude longpolling = location. getlongpolling (); // latitude }};/*** second method for enabling and disabling gps * private void openGPSSettings () {// get the current GPS status (ON or OFF) boolean gpsEnabled = Settings. secure. isLocationProviderEnabled (getContentResolver (), LocationManager. GPS_PROVIDER); if (gpsEnabled) {// disable GPSSettings. secure. setLocationProviderEnabled (getContentResolver (), LocationManager. GPS_PROVIDER, false);} else {// enable GPS Settings. secure. setLocationProviderEnabled (getContentResolver (), LocationManager. GPS_PROVIDER, true );}}*/}
The main. xml layout is as follows:

     
  
 
The configuration file is as follows:

 
     
        
      
      
      
      
                          
                                  
               
              
 
The running result is as follows:

Download the Demo. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + tdq2/tbWt723qKOosNm2yLXYzbxBUEnKtc/samples + CjxwPs/C1NhEZW1vx + vDzbTBPGJyPgo8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20141012/2014101209154556.gif" alt = "\">

Method 3 (MAP world API implementation)

Download the Demo.



Related Article

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.