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 support, and the program does not create Locationmanager instances directly, but is obtained through the context's Getsystemservice () method.

For example: 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

 PackageSn.qdj.localgpsdemo;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.location.Location;ImportAndroid.location.LocationListener;ImportAndroid.location.LocationManager;ImportAndroid.os.Bundle;ImportAndroid.widget.EditText;/*** GPS Positioning *@authorQingdujun **/ Public classMainactivityextendsActivity {Locationmanager lm;        EditText Show; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Show=(EditText) Findviewbyid (r.id.show); //Create a Locationmanager objectLM =(Locationmanager) Getsystemservice (Context.location_service); //Get recent location information from the GPSLocation LC =lm.getlastknownlocation (Locationmanager.gps_provider); //Update display location informationUpdateview (LC); //Set GPS location information once every 3 secondsLm.requestlocationupdates (Locationmanager.gps_provider, 3000, 8,NewLocationlistener () {@Override Public voidOnstatuschanged (String provider,intstatus, Bundle extras) {                //TODO auto-generated Method Stub} @Override Public voidonproviderenabled (String provider) {//when the GPS Locationprovider is available, update the locationUpdateview (lm.getlastknownlocation (provider)); } @Override Public voidonproviderdisabled (String provider) {//TODO auto-generated Method StubUpdateview (NULL); } @Override Public voidonlocationchanged (location location) {//when GPS location information changes, update the locationUpdateview (location);            }        }); }         Public voidUpdateview (location newlocation) {if(NewLocation! =NULL) {StringBuilder sb=NewStringBuilder (); 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

<Relativelayoutxmlns: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 -    <EditTextAndroid:id= "@+id/show"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:hint="" /></Relativelayout>

GPS location needs to add a permission

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

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.