Android GPS Location

Source: Internet
Author: User

GPS positioning seems to be in the room, today I got a GPS positioning small demo, including the user's longitude, latitude, height, direction, movement speed, accuracy and other information. Android provides a Locationmanager class specifically for GPS feature support, and the program does not directly create Locationmanager instances, Instead, it is obtained through the Getsystemservice () method of the context.

For example:

Create Locationmanager object Locationmanager lm = (locationmanager) getsystemservice (Context.location_service);

The following program is simple, and the layout only uses one edittext to display all the data:


Example 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 location * @author Qingdujun * */public class mainactivity Exte    NDS Activity {Locationmanager lm;        EditText Show;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);                Show = (EditText) Findviewbyid (r.id.show);        Create Locationmanager Object LM = (Locationmanager) getsystemservice (Context.location_service);                Obtaining the nearest location information from GPS position LC = Lm.getlastknownlocation (Locationmanager.gps_provider);                Update display location information Updateview (LC); Set the GPS location information to get once every 3 seconds lm.requestlocationupdates (Locationmanager.gps_provider, 8, New Locationlistener () {@Overr Idepublicvoid Onstatuschanged (String provider, int status, Bundle extras) {//TODO auto-generated method stub} @Overridepublic void Onproviderenabled (String Provider) {//When the GPS Locationprovider is available, update the location Updateview (lm.getlastknownlocation (provider));} @Overridepublic void onproviderdisabled (String provider) {//TODO auto-generated method Stubupdateview (null);}            @Overridepublic void onlocationchanged (location) {//When the GPS location information is changed, update the positioning Updateview;}}); } public void Updateview (location newlocation) {if (newlocation! = null) {StringBuilder SB = new StringBuilder () ; Sb.append ("real-time location information: \ n"); Sb.append ("Longitude: \ n"); Sb.append (Newlocation.getlongitude ()); Sb.append ("\ n Latitude:"); Sb.append ( Newlocation.getlatitude ()); Sb.append ("\ n height:"); Sb.append (Newlocation.getaltitude ()); Sb.append ("\ n speed:"); Sb.append (Newlocation.getspeed ()); Sb.append ("\ n Direction:"); Sb.append (newlocation.getbearing ()); Sb.append ("\ n positioning accuracy:"); Sb.append (Newlocation.getaccuracy ()); Show.settext (sb.tostring ());} else {show.settext (null);}    }} 

Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= "Sn.qdj.localgpsdemo.MainActivity" >    <!--display positioning information-    <edittext        Android : id= "@+id/show"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android: Hint= ""/></relativelayout>
GPS location needs to add a permission

<!--GPS positioning--    <uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>

reference article: "Crazy Android Handout (2nd edition)" by Li Gang

Android GPS Location

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.