Today, one hours, wrote a GPS acquisition location code of a small example, including a reference to some of the code on the Web, and some changes to the code, I hope to help. The specific code is as follows: to practical adnroid platform GPS equipment, first need to add permissions, so you need to 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 open:
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, "Please open gps! ", Toast.length_short). Show ();
Intent Intent = new Intent (settings.action_security_settings);
Startactivityforresult (intent,0); This is returned to the Get interface when the setting is complete
}
If turned on, it will go directly to the Display page, if it is not normal, it will go to the GPS Settings page:
Get the code as follows:
private void GetLocation ()
{
//Get Position Management Service
Locationmanager Locationmanager;
String serviceName = Context.location_service;
Locationmanager = (Locationmanager) this.getsystemservice (serviceName);
Find a service information criteria
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
String Provider = Locationmanager.getbestprovider (criteria, true);//Get GPS information
Location Location = Locationmanager.getlastknownlocation (provider); Acquiring position
Updatetonewlocation (location) via GPS;
Set the listener, the minimum time for automatic Updates is interval n seconds (1 seconds for 1*1000, which is written mainly for convenience) or minimum displacement change over n m
locationmanager.requestlocationupdates (provider, 1000,
locationlistener);
Here you can get the location information, but still show it, then use the following method to display:
private void Updatetonewlocation (Location Location) {
TextView tv1;
TV1 = (TextView) This.findviewbyid (R.ID.TV1);
if (location!= null) {
double latitude = location.getlatitude ();
Double longitude= location.getlongitude ();
Tv1.settext ("Latitude:" + latitude+ "N Longitude" + longitude);
} else {
Tv1.settext ("Cannot get geographic information");
}
This way you can get to the current user's location, at least how to implement the map, in the following will be captured and displayed! Thanks to the people who are referencing the code!