Use of GPs in Android and resolution NMEA0183 protocol

Source: Internet
Author: User

The graduation design needs to use the Android GPS location, summarizes these days to learn about the GPS related.

For testing, the layout file is simple and only two TextView

<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"Android:keepscreenon= "true"Tools:context= "Com.catcher.testcompass.MainActivity" >    <TextViewAndroid:id= "@+id/tv_rgs84"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Elevation" />    <ScrollViewAndroid:id= "@+id/scrollview1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_below= "@+id/tv_rgs84"Android:layout_margintop= "80DP" >        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >            <TextViewAndroid:id= "@+id/tv_nmea"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "NMEA" />        </LinearLayout>    </ScrollView></Relativelayout>

Specific code implementation

 PackageCom.catcher.testcompass;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.hardware.GeomagneticField;ImportAndroid.location.GpsStatus.NmeaListener;Importandroid.location.Location;ImportAndroid.location.LocationListener;ImportAndroid.location.LocationManager;ImportAndroid.os.Bundle;Importandroid.provider.Settings;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classSecondactivityextendsActivity {PrivateTextView tvWGS84, Tvnmea; PrivateLocationlistener Gpslistener; PrivateLocationmanager Mlocationmanager; PrivateGeomagneticfield Gmfield; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_second); //Show WGS84 DatatvWGS84 =(TextView) Findviewbyid (R.ID.TV_RGS84); //displaying the data in the NMEA protocolTvnmea =(TextView) Findviewbyid (R.ID.TV_NMEA); Mlocationmanager=( (Locationmanager) Getsystemservice (Context.location_service)); Mlocationmanager.addnmealistener (NewNmealistener () {@Override Public voidOnnmeareceived (Longtimestamp, String NMEA)                {tvnmea.invalidate (); //here take Gpgga as an example//$GPGGA, 232427.000,3751.1956,n,11231.1494,e,1,6,1.20,824.4,m,-23.0,m,,*7e                if(Nmea.contains ("Gpgga") ) {String info[]= Nmea.split (","); //Gpgga Altitude is the MSL altitude (mean sea level)Tvnmea.settext ("Number of satellites in use" + info[7] + "\ n Altitude" + info[9]                            + "\ n the height WGS84 geoid of the earth ellipsoid to the geoid" + info[11]);        }            }        }); Gpslistener=NewMylocationlistner (); }    Private classMylocationlistnerImplementsLocationlistener {@Override Public voidonlocationchanged (location location) {tvwgs84.invalidate ();            Tvnmea.invalidate (); Double Longitude=Location.getlongitude (); floataccuracy =location.getaccuracy (); Double Latitude=Location.getlatitude (); Double Altitude= Location.getaltitude ();//WGS84            floatBearing =location.getbearing (); Gmfield=NewGeomagneticfield ((float) Location.getlatitude (), (float) Location.getlongitude (), (float) Location.getaltitude (), System.currenttimemillis ()); Tvwgs84.settext ("Altitude=" + Altitude + "\nlongitude=" + Longitude + "\nlatitude=" + latitude + "\ndeclination=" + GMF Ield.getdeclination () + "\nbearing=" + bearing + "\naccuracy=" +accuracy); } @Override Public voidOnstatuschanged (String provider,intstatus, Bundle extras) {} @Override Public voidonproviderenabled (String provider) {} @Override Public voidonproviderdisabled (String provider) {}} @Overrideprotected voidOnPause () {Super. OnPause (); //no longer positioned after exiting activitymlocationmanager.removeupdates (Gpslistener); } @Overrideprotected voidOnresume () {Super. Onresume (); //determine if GPS is available        if(mlocationmanager.isproviderenabled (Locationmanager.gps_provider)) {Toast.maketext ( This, "GPS Available", Toast.length_long). Show (); //Start PositioningMlocationmanager.requestlocationupdates (locationmanager.gps_provider, 1000, 0, Gpslistener); }Else{Toast.maketext ( This, "Please open GPS or select GPS mode for high accuracy", Toast.length_long). Show (); //go to set up a GPS pageStartActivity (NewIntent (settings.action_location_source_settings)); }    }}

Androidmanifest Adding permissions

<!--This permission is used for network positioning -<uses-permissionAndroid:name= "Android.permission.ACCESS_COARSE_LOCATION" ></uses-permission><!--This permission is used to access GPS location -<uses-permissionAndroid:name= "Android.permission.ACCESS_FINE_LOCATION" ></uses-permission>

Use of GPs in Android and resolution NMEA0183 protocol

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.