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!