Acquisition and display of GPS signals on Android platforms

Source: Internet
Author: User

From: http://www.linuxidc.com/Linux/2011-09/42454.htm

Concepts and basic classes

Recently I learned GPS, so I made a GPS signal parsing tool on the Android system (HTC G7 mobile phone). The following is a summary. Thank you for your correction.

Function:

1. obtained the location GPS information, including longitude and latitude, speed, and direction.

2. Satellite information, including the number of satellites, elevation angle, azimuth angle, and signal-to-noise ratio.

And drew a map of the shape of the earth to show satellite information, because there is no time, not to obtain the Magnetic Field Sensor Information

First, let's take a look at the basic concepts of GPS:

GPS, short for global positioning system, is a global positioning system designed to accurately locate and detect ground and air targets worldwide.

Generally, the GPS system consists of three parts: the ground control station, navigation satellite, and receiver.

Mobile phones with the GPS function must first have a GPS module to receive GPS signals. The GPS module communicates with the microprocessor control module through asynchronous serial communication. The GPS module sends GPS data to the serial port every 1 second.

On the Android platform, the system has done how to obtain GPS data from the window. We provide interfaces to obtain GPS data by calling these interfaces instead of reading the serial port; if you want to obtain GPS data on the wince platform, you must read the serial port, and the serial port in wince is an exclusive device. If you enable the serial port in the program for data monitoring, before you open the navigation software, you must disable the serial port. Of course, you can also use the virtual serial port to allow multiple processes to obtain data at the same time. I only need to provide some main modules below.

Main classes used:

Locationmanager;

Location;

Gpsstatus;

Criteria;

Main variable definitions:

Private location;
// Locate the management class
Private locationmanager;
// Monitors satellite Variables
Private gpsstatus;
Iterable <gpssatellite> allsatellites;
Protected iterator <gpssatellite> iteratorsate;

Private criteria;

1. Enable the GPS service. locationmanger is the location management service, which requires both GPS information and satellite information:

Locationmanager; private void opengpssettings () {// obtain location management service locationmanager = (locationmanager) This. getsystemservice (context. location_service); If (locationmanager. isproviderenabled (Android. location. locationmanager. gps_provider) {toast. maketext (this, "GPS module normal", toast. length_short ). show (); return;} status = false; toast. maketext (this, "Turn on GPS! ", Toast. length_short ). show (); intent = new intent (settings. action_security_settings); startactivityforresult (intent, 0); // return to the get interface after the setting is complete}

2. Fill in the criteria location data standard class, call the location listening function and satellite listening function, set the settings to be operated in the criteria data standard class, and use

Private void getlocation () {// find the service information location data standard class Criteria = new criteria (); // query accuracy: high criteria. setaccuracy (criteria. accuracy_fine); // whether to query the altitude: criteria. setaltituderequired (true); // whether to query the azimuth angle: criteria. setbearingrequired (true); // whether to allow paid criteria. setcostallowed (true); // power requirement: low criteria. setpowerrequirement (criteria. power_low); // query speed: criteria. setspeedrequired (true); provider = locationmanager. getbestprovider (criteria, true); // obtain GPS information. Obtain location information in location provider. Location = locationmanager. getlastknownlocation (provider); // get the location updatetonewlocation (location) through GPS; // set the listener. The minimum interval for automatic update is n seconds (1 second is 1*1000, in this way, the write is mainly for convenience) or the minimum displacement is greater than N meters. // the data in the location provider is obtained in real time. Once the location change occurs, the application locationlistener locationmanager is notified immediately. requestlocationupdates (provider, 1000, 0, locationlistener); // listens to the satellite. statuslistener is the response function locationmanager. addgpsstatuslistener (statuslistener );}

Response and Signal Analysis

The following is the response function of the listener function:

1. Location response function. When the mobile terminal moves, the location changes and the following location response function is triggered.

// The location listening class monitors changes in location information. Private Final locationlistener = new locationlistener () {// when the location changes, call the following function @ override public void onlocationchanged (location) {// obtain the location through GPS and place the new location information in the location, call the updatetonewlocation function to display the location information. updatetonewlocation (location);} // when the provider is unavailable, call the following function @ override public void onproviderdisabled (string arg0) {}@ override public void onproviderenabled (string arg0) {}@ override public void onstatuschanged (string arg0, int arg1, bundle arg2) {updatetonewlocation (null );}};

2. Display GPS location information. In the location response function, the following function is called when location changes.

Private void updatetonewlocation (location) {If (location! = NULL) {bear = location. getbearing (); // float latitude = (float) location. getlatitude (); // dimension float longpolling = (float) location. getlongpolling (); // longitude float gpsspeed = (float) location. getspeed (); // speed long gpstime = location. gettime (); // time date = new date (gpstime); // use date to convert the time into dateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS "); // you can set the time display format to: yyyy/mm/dd hh: mm: SS float gpsalt = (float) location. getaltitude (); // latitudeview at an altitude. settext ("" + latitude); longitudeview. settext ("" + longpolling); speedview. settext ("" + gpsspeed); timeview. settext ("" + DF. format (date); altitudeview. settext ("" + gpsalt); bearingview. settext ("" + bear);} else {toast. maketext (this, "unable to obtain geographic information", toast. length_short ). show ();}}}

3. Status response function. When the received satellite parameter changes, the following response function is called.

// Add the listening satellite private final gpsstatus. listener statuslistener = new gpsstatus. listener () {@ override public void ongpsstatuschanged (INT event) {// todo auto-generated method stub // obtain GPS satellite information, which is the same as location information, or through the locationmanager class, the method is getgpsstatus, and a gpsstatus type structure gpsstatus = locationmanager is returned. getgpsstatus (null); // trigger event switch (event) {Case gpsstatus. gps_event_started: break; // The first position time case gpsstatus. gps_event_first_fix: break; // receives satellite information and calls the drawmap () function to parse satellite signals and display them on the screen. gps_event_satellite_status: drawmap (); break; Case gpsstatus. gps_event_stopped: break ;}};

4. Satellite Signal Analysis

Private int heightp; private int widthp; protected void drawmap () {// todo auto-generated method stub int I = 0; // obtain screen information, dm = new displaymetrics (); getwindowmanager (). getdefadisplay display (). getmetrics (DM); // call these three functions to save the screen information in DM heightp = DM. heightpixels; // cell phone screen height widthp = DM. widthpixels; // cell phone screen width // obtain satellite information. gpsstatus indicates the satellite gpsstatus variable allsatellites = gpsstatus in the satellite response function. getsatellites (); iteratorsate = allsatellites. iterator (); // traverses the information of each satellite while (iteratorsate. hasnext () {gpssatellite satellite = iteratorsate. next (); alimuth [I] = satellite. getazimuth (); // coordinate azimuth elevation [I] = satellite. getelevation (); // satellite elevation SNR [I] = satellite. getsnr (); I ++;} satcountview. settext ("" + I); // layout is the class inherited from the view, and the redraw is called to display the satellite information layout. redraw (Bear, alimuth, elevation, SNR, widthp, heightp, I); // call the invalidate () method to refresh the view and display the drawn information layout. invalidate ();}

Configuration and simulator settings

1. Modify the permission in the file androidmanifest. xml:

In uses-permission, the Code is as follows:

<?xml version="1.0" encoding="utf-8"?>   <manifest xmlns:Android="http://schemas.android.com/apk/res/android"         package="zfy.mygps"         Android:versionCode="1"         Android:versionName="1.0">       <uses-sdk Android:minSdkVersion="8" />          <application Android:icon="@drawable/icon" android:label="@string/app_name"               Android:debuggable="true">           <activity Android:name=".Mygps"                     Android:label="@string/app_name"                     Android:screenOrientation="portrait">               <intent-filter>                   <action Android:name="android.intent.action.MAIN" />                   <category Android:name="android.intent.category.LAUNCHER" />               </intent-filter>           </activity>           </application>          <uses-permission Android:name="android.permission.ACCESS_FINE_LOCATION" />        <uses-permission Android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>   </manifest>  

2. If you want to run in the simulator, you also need the simulator to support GPS, which may not be enabled when you enable the simulator. The simulator's support for GPS is as follows:

In eclipse: Window ---> Android SDK and AVD manager. Open the simulator Management window. If there is an available simulator, open edit. If not, create a new simulator, for example:

In the window on the right of hardware, You can see whether GPS is supported. If not, click New on the right to bring up the following window:

Select the GPS support and exit.

In this way, the simulator supports GPS. You can use the simulator to test your program,

3. Use emulator control in eclipse to test positioning;

Window --> show View --> emulator control (if there is no emulator control in show view, it is in the other menu). If the simulator is not enabled, the emulator control is gray and cannot be edited, if the simulator is gray after it is enabled, restart eclipse (do not turn off the simulator)

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.