Implementation of Android GPS positioning (1)

Source: Internet
Author: User

Today, I spent more than an hour writing a GPS to get the geographic location.CodeSmall examples, including some online code and some modifications to the code, I hope to help you. The specific code is as follows: to use the GPS device of the adnroid platform, you must first add the permission. Therefore, you must add the following permissions:

 

 
  <Uses-Permission Android: Name="Android. Permission. access_fine_location"> </Uses-Permission>

 

The specific implementation code is as follows:

First, determine whether the GPS module exists or is Enabled:

 

Code

  Private     Void  Opengpssettings (){
Locationmanager ALM = (Locationmanager) This
. Getsystemservice (context. location_service );
If (ALM
. Isproviderenabled (Android. Location. locationmanager. gps_provider )){
Toast. maketext ( This , " GPS module normal " , Toast. length_short)
. Show ();
Return ;
}

Toast. maketext ( This , " Turn on GPS! " , Toast. length_short). Show ();
Intent intent = New Intent (settings. action_security_settings );
Startactivityforresult (intent, 0 ); // Return to the get interface after setting.

}

 

If the function is enabled normally, the display page is displayed. If the function is enabled improperly, the GPS settings page is displayed:

The Code is as follows:

 

Code

  Private     Void  Getlocation ()
{
// Get Location Management Service
Locationmanager;
String servicename = Context. location_service;
Locationmanager = (Locationmanager) This . Getsystemservice (servicename );
// Find Service Information
Criteria = New Criteria ();
Criteria. setaccuracy (criteria. accuracy_fine ); // High Precision
Criteria. setaltituderequired ( False );
Criteria. setbearingrequired ( False );
Criteria. setcostallowed ( True );
Criteria. setpowerrequirement (criteria. power_low ); // Low Power Consumption

String provider = Locationmanager. getbestprovider (criteria, True ); // Get GPS information
Location = Locationmanager. getlastknownlocation (provider ); // Get location through GPS
Updatetonewlocation (location );
// Set the listener. The minimum time for automatic update is n seconds (1 second is 1*1000, which is mainly used for convenience) or the minimum displacement is greater than N meters.
Locationmanager. requestlocationupdates (provider, 100 * 1000 , 500 ,
Locationlistener ); }

Here we can get the geographic location information, but we still need to display it, so we can use the following method to display it:

 

Code

  Private     Void Updatetonewlocation (location ){

Textview TV1;
TV1 = (Textview) This . Findviewbyid (R. Id. TV1 );
If (Location ! = Null ){
Double Latitude = Location. getlatitude ();
Double Longpolling = Location. getlongpolling ();
Tv1.settext ( " Dimension: " + Latitude + " \ N longitude " + Longpolling );
} Else {
Tv1.settext ( " Unable to obtain Geographic Information " );
}

}

 

In this way, you can get the geographic location of the current user. At least how to implement the map below will be obtained and displayed below! Thank you for your reference code!

 

 

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.