Android GPS application: dynamically obtains Location Information

Source: Internet
Author: User

This article introduces the concept of GPS and common classes and methods involved in GPS application development in Android. In this article, a small application is developed to obtain positioning information in real time, including the user's latitude, longitude, height, direction, and moving speed. The Code is as follows: Activity: [java] package comhome. location; 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; public class LocationTestActivity extends Activity {// defines the LocationManager object private LocationManager locationManager; Private EditText show; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); show = (EditText) findViewById (R. id. main_et_show); // obtain the system LocationManager service locationManager = (LocationManager) getSystemService (Context. LOCATION_SERVICE); // obtain the latest Location information from GPS. location Location = locationManager. getLastKnownLocation (LocationManager. GPS_PROVIDER); // display the location information in the location in the EditText updateView (location); // set the location manager to get the GPS location information every 2 seconds. requestLocationUpdates (LocationManager. GPS_PROVIDER, 2000, 8, new LocationListener () {@ Override public void onLocationChanged (Location location) {// update the location updateView (Location) when the GPS location information changes );} @ Override public void onProviderDisabled (String provider) {updateView (null) ;}@ Override public voi D onProviderEnabled (String provider) {// when the GPS LocationProvider is available, update the location updateView (locationManager. getLastKnownLocation (provider);} @ Override public void onStatusChanged (String provider, int status, Bundle extras) {}});} private void updateView (Location location) {if (Location! = Null) {StringBuffer sb = new StringBuffer (); sb. append ("real-time location information: \ n longitude:"); sb. append (location. getlongpolling (); sb. append ("\ n latitude:"); sb. append (location. getLatitude (); sb. append ("\ n Height:"); sb. append (location. getAltitude (); sb. append ("\ n speed:"); sb. append (location. getSpeed (); sb. append ("\ n Direction:"); sb. append (location. getBearing (); sb. append ("\ n precision:"); sb. append (location. getAccuracy (); show. setText (sb. to String ();} else {// clear EditText show if the input Location object is empty. setText ("") ;}} package comhome. location; 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; public class LocationTestActivity extends Activity {// define LocationMan Manager object private LocationManager locationManager; private EditText show; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); show = (EditText) findViewById (R. id. main_et_show); // obtain the system LocationManager service locationManager = (LocationManager) getSystemService (Context. LOCATION_SERVICE); // obtain the latest Location information from GPS. location Location = locationManager. GetLastKnownLocation (LocationManager. GPS_PROVIDER); // display the location information in the location in the EditText updateView (location); // set the location manager to get the GPS location information every 2 seconds. requestLocationUpdates (LocationManager. GPS_PROVIDER, 2000, 8, new LocationListener () {@ Overridepublic void onLocationChanged (Location location) {// update the location updateView (Location) when the GPS location information changes );} @ Overridepublic void onProviderDisabled (String provider) {updateView (null) ;}@ Overridepublic void onProviderEnabled (String provider) {// when the GPS LocationProvider is available, update the location updateView (locationManager. getLastKnownLocation (provider);} @ Overridepublic void onStatusChanged (String provider, int status, Bundle extras) {}});} private void updateView (Location location) {if (Location! = Null) {StringBuffer sb = new StringBuffer (); sb. append ("real-time location information: \ n longitude:"); sb. append (location. getlongpolling (); sb. append ("\ n latitude:"); sb. append (location. getLatitude (); sb. append ("\ n Height:"); sb. append (location. getAltitude (); sb. append ("\ n speed:"); sb. append (location. getSpeed (); sb. append ("\ n Direction:"); sb. append (location. getBearing (); sb. append ("\ n precision:"); sb. append (location. getAccuracy (); show. setText (sb. toString ();} else {// clear EditTextshow if the input Location object is empty. setText ("") ;}} layout XML: [html] <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <EditText android: id = "@ + id/main_et_show" android: layout_width = "match_parent" android: layout_height = "match_parent" android: cursorVisible = "false" android: editable = "false"/> </LinearLayout> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <EditText android: id = "@ + id/main_et_show" android: layout_width = "match_parent" android: layout_height = "match_parent" android: cursorVisible = "false" android: editable = "false"/> </LinearLayout> permission: [html] <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION "/> <uses-permission android: name =" android. permission. ACCESS_FINE_LOCATION "/> effect:

 

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.