Android GPS Positioning

Source: Internet
Author: User

Android GPS Positioning

GPS Positioning seems to be unavailable in the room. Today I made a GPS Positioning Demo, including the user's longitude, latitude, height, direction, moving speed, accuracy, and other information. Android provides a dedicated LocationManager class for the GPS function. The program cannot directly create a LocationManager instance, but is obtained through the getSystemService () method of Context.

For example:

 

// Create the LocationManager object LocationManager lm = (LocationManager) getSystemService (Context. LOCATION_SERVICE );

 

The following program is simple. Only one EditText is used in the layout to display all data:

 

Instance Demo:

MainActivity. java

 

Package sn. qdj. localgpsdemo; import android. app. activity; import android. content. context; import android. location. location; import android. location. locationListener; import android. location. locationManager; import android. OS. bundle; import android. widget. editText;/*** GPS Positioning * @ author qingdujun **/public class MainActivity extends Activity {LocationManager lm; EditText show; @ Override protected void onCr Eate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); show = (EditText) findViewById (R. id. show); // create the LocationManager object lm = (LocationManager) getSystemService (Context. LOCATION_SERVICE); // obtain the latest Location information from GPS. Location lc = lm. getLastKnownLocation (LocationManager. GPS_PROVIDER); // update and display the positioning information updateView (lc); // you can obtain the GPS positioning information every 3 seconds. requestLocationUpdates (Lo CationManager. GPS_PROVIDER, 3000, 8, new LocationListener () {@ Overridepublic void onStatusChanged (String provider, int status, Bundle extras) {// TODO Auto-generated method stub} @ Overridepublic void onProviderEnabled (String provider) {// When GPS LocationProvider is available, update and locate updateView (lm. getLastKnownLocation (provider);} @ Overridepublic void onProviderDisabled (String provider) {// TODO Auto-generated metho D stubupdateView (null) ;}@ Overridepublic void onLocationChanged (Location location) {// when the GPS positioning information changes, update and locate updateView (location );}});} public void updateView (Location newLocation) {if (newLocation! = Null) {StringBuilder sb = new StringBuilder (); sb. append (Real-time location information :); sb. append (longitude :); sb. append (newLocation. getlongpolling (); sb. append (latitude :); sb. append (newLocation. getLatitude (); sb. append (height :); sb. append (newLocation. getAltitude (); sb. append (speed :); sb. append (newLocation. getSpeed (); sb. append (Direction :); sb. append (newLocation. getBearing (); sb. append (positioning precision :); sb. append (newLocation. getAccuracy (); show. setText (sb. toString ();} else {show. setText (null );}}}

Activity_main.xml

 

     
      
  
 
You need to add a permission to GPS positioning.

 

     
 

 

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.