Android about Locationmanager GPS positioning
The Locationmanager provides two ways to locate the current location GPS and network where GPS error about 50m around the network about 500m
At first, when the GPS was turned on I used location location = locationmanager.getlastknownlocation (Locationmanager.gps_provider); But has been null do not know what the reason for searching the internet for a long time finally found a solution
Android has a criteria class that can directly determine the current suitability for GPs or network and set up the Locationlistener listener to update location code directly on the real-time
private void Getlocaladdress () {
Locationmanager Locationmanager;
Locationmanager = (locationmanager) getsystemservice (Context.location_service);
criteria = new criteria ();
criteria.setaccuracy (criteria.accuracy_fine);
criteria.setaltituderequired (false);
criteria.setbearingrequired (false);
criteria.setcostallowed (true);
criteria.setpowerrequirement (Criteria.power_low);
String Provider = Locationmanager.getbestprovider (criteria, true);
Location location = locationmanager.getlastknownlocation (provider);
//updatewithnewlocation (location);
Locationmanager.requestlocationupdates (provider, 2000, 10,
Locationlistener);
updatewithnewlocation (location);
}
Private final Locationlistener Locationlistener = new Locationlistener () {
public void onlocationchanged (location location) {
updatewithnewlocation (location);
}
public void onproviderdisabled (String provider) {
updatewithnewlocation (null);
}
public void onproviderenabled (String provider) {
}
public void onstatuschanged (String provider, int status, Bundle extras) {
}
};
private void Updatewithnewlocation (location location) {
String latlongstring;
TextView Mylocationtext;
if (location! = null) {
Double lat = location.getlatitude ();
Double LNG = location.getlongitude ();
latlongstring = "Latitude:" + lat + "\ n Longitude:" + LNG;
} else {
latlongstring = "Unable to obtain geographic information";
}
Toast.maketext (searchxiaoquactivity.this, latlongstring,
Toast.length_long). Show ();
}
An issue that may occur with Android targeting the current location