Android GPS Programming

Source: Internet
Author: User

1. enable the system GPS 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;
}

2. Set service information to be queried (fill location data class criteria)

// Query accuracy: high
Criteria. setaccuracy (criteria. accuracy_fine );
// Whether to query the altitude: Yes
Criteria. setaltituderequired (true );
// Whether to query the azimuth angle: Yes
Criteria. setbearingrequired (true );
// Whether payment is allowed
Criteria. setcostallowed (true );
// Power requirement: Bottom
Criteria. setpowerrequirement (criteria. power_low );
// Query speed: Yes
Criteria. setspeedrequired (true );

Set the information to be queried to true.

3. Get location

// Obtain the location information in the location provider

Provider = locationmanager. getbestprovider (criteria, true );

4. Set the location listener to update the GPS information in real time when the location changes

Locationmanager. requestlocationupdates (provider, 1000, 0, locationlistener); // The second parameter is the update time (in milliseconds), the second parameter is the update distance, and the last parameter is the listener function.

5. Set listener Functions

// The location listener class monitors the changes in location information.
Private Final locationlistener = new locationlistener ()
{

@ Override
Public void onlocationchanged (location)
{
// Obtain the location through GPS
Updatetonewlocation (location); // displays GPS information
}

@ Override
Public void onproviderdisabled (string arg0)
{

}

@ Override
Public void onproviderenabled (string arg0)
{

}

@ Override
Public void onstatuschanged (string arg0, int arg1, bundle arg2)
{
Updatetonewlocation (null );
}
};

 

6. Display GPS information

Private void updatetonewlocation (location)
{

If (location! = NULL)
{
Bear = location. getbearing (); // degree different from the positive north
Double latitude = location. getlatitude (); // dimension
Double longpolling = location. getlongpolling (); // longitude
Double gpsspeed = location. getspeed (); // speed
Long gpstime = location. gettime (); // time
Date = new date (gpstime );
Dateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");

Float gpsalt = (float) location. getaltitude (); // altitude
Latitudeview. 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 ();
}

}

7. Apart from the masterProgramTo obtain GPS information, you must add permissions to the manifest file.

<? 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" type = "codeph" text = "/codeph">
<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>

 

 

The locationmanager class is also used to obtain satellite information, where addgpsstatuslistener () is used to listen:

Locationmanager. addgpsstatuslistener (statuslistener ). The parameter is a listener function.

 

 

Listener function:

// Add a monitoring satellite
Private Final gpsstatus. Listener statuslistener = new gpsstatus. listener (){
@ Override
Public void ongpsstatuschanged (INT event ){
// Todo auto-generated method stub
// Obtain GPS satellite information
Gpsstatus = locationmanager. getgpsstatus (null );
Switch (Event)
{
Case gpsstatus. gps_event_started:
Break;
// The first positioning time
Case gpsstatus. gps_event_first_fix:
Break;
// Received satellite information
Case gpsstatus. gps_event_satellite_status:

{

// Retrieve all satellites
Allsatellites = gpsstatus. getsatellites ();

// Obtain the satellite Sequence
Iteratorsate = allsatellites. iterator ();

While (iteratorsate. hasnext ())
{
Gpssatellite satellite = iteratorsate. Next ();
Satellite. getazimuth (); // satellite Azimuth
Satellite. getelevation (); // satellite elevation
Satellite. getsnr (); // Signal-to-Noise Ratio

Satellite. getprn (); // pseudo-random number
}

}
Break;
Case gpsstatus. gps_event_stopped:
Break;
}
}
};

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.