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