Copy Code code as follows:
Declaring Locationmanager objects
Locationmanager Loctionmanager;
Access to Locationmanager objects through system services
Loctionmanager = (Locationmanager) getsystemservice (Context.location_service);
Mode one:
Copy Code code as follows:
Get location via GPS location provider
String Providergps = Locationmanager.gps_provider;
Location Location = loctionmanager.getlastknownlocation (PROVIDERGPS);
Mode two:
Copy Code code as follows:
Get location from Base station location provider
String providernetwork = Locationmanager.network_provider;
Location Location = loctionmanager.getlastknownlocation (providernetwork);
Mode three:
Copy Code code as follows:
Use standard collections to allow the system to automatically select the best available location provider and provide location
Criteria criteria = new criteria ();
Criteria.setaccuracy (criteria.accuracy_fine);//High precision
Criteria.setaltituderequired (false);/No elevation required
Criteria.setbearingrequired (false);/No Azimuth required
Criteria.setcostallowed (TRUE);//Allow cost
Criteria.setpowerrequirement (Criteria.power_low);/Low power consumption
Match the best provider of the above criteria from the available location providers
String Provider = Loctionmanager.getbestprovider (criteria, true);
Get the last change position
Location Location = loctionmanager.getlastknownlocation (provider);
Processing:
Copy Code code as follows:
Show in EditText
Updatewithnewlocation (location);
Monitoring position change, 2 seconds, distance 10 meters or more
Loctionmanager.requestlocationupdates (provider, 1000, 1, Locationlistener);
Listeners and display:
Copy Code code as follows:
//position Listener
Private final Locationlistener Locationlistener = new locationl Istener () {
@Override
public void onstatuschanged (String provider, int status, Bundle extras) {
}
@Override
public void onproviderenabled (String provider) {
}
@Override
public void Onproviderdisab LED (String provider) {
}
///When position changes trigger
@Override
public void onlocationchanged (Location Location) {
//Use the new location update TextView display
Updatewithnewlocation (location);
}
};
private void Updatewithnewlocation (Location Location) {
if (Location!= null) {
Double lat = location.getl Atitude ();
Double LNG = location.getlongitude ();
Latstr = Format.format (LAT);
Lonstr = Format.format (LNG);
Txtlat.settext (LATSTR);
Txtlon.settext (LONSTR);
} else {
Txtlat.settext ("");
Txtlon.settext ("");
}
}