1:getlocation () method for selecting optimal latitude and longitude by sieve
2:mapthread thread gets the city name by uploading the latitude and longitude obtained by the getLocation ()
public class Positionactivity extends Baseactivity implements Iinit, Iresponsehandler, View.onclicklistener {private T Extview Mlocationtv, mcategorytv;//position. Type private double latitude, longitude;//latitude private String mapuristr = "http:/ /MAPS.GOOGLE.CN/MAPS/API/GEOCODE/JSON?LATLNG={0},{1}&SENSOR=TRUE&LANGUAGE=ZH-CN "; Private HttpResponse HttpResponse = null; Private httpentity httpentity = null; Private Mapthread Mapthread; Private Handler Handler; Private String result; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_share_goods_edit); Init (); } @Override public void init () {Mlocationtv = (TextView) Findviewbyid (r.id.tv_goods_location);, Getlo cation ();//Get latitude and longitude Mapthread = new Mapthread (); Mapthread.start (); }//Get latitude and longitude public void getLocation () {Locationmanager LM = (Locationmanager) GetsystemseRvice (Context.location_service); List<string> LP = Lm.getallproviders ();//Returns a list of names of all known location providers, including unauthorized access or call activity that is currently deactivated. for (String ITEM:LP) {//LOG.I ("----> Available Location Services:", item); } criteria = new criteria (); Criteria.setcostallowed (false);//Set Location service free criteria.setaccuracy (criteria.accuracy_coarse); Sets the horizontal position accuracy of String ProviderName = Lm.getbestprovider (criteria, true); Getbestprovider only the location that allows access to the calling activity the provider will be returned if (providerName! = null) {Site location = Lm.getlastknownlo cation (providerName); Latitude = Location.getlatitude ();//Get dimension information longitude = Location.getlongitude ();//Get longitude information} else { Toastutil.show (This, "1. Please check the network connection \n2. Please open my location"); }} class Mapthread extends Thread {@Override public void run () {super.run (); String uristr = Messageformat.format (mapuristr, latitude, longitude); HttpGet HttpGet = new HTTPGET (URISTR);//Generate a Request object HttpClient HttpClient = new Defaulthttpclient (); Generate an HTTP client object try {HttpResponse = Httpclient.execute (httpget);//Send Request object using HTTP Client Httpentity = Httpresponse.getentity (); Get response content BufferedReader reader = new BufferedReader (New InputStreamReader (Httpentity.getcontent ())); result = ""; String line = ""; while (line = Reader.readline ()) = null) {result + = line; }LOG.V ("Address:", result); } catch (Exception e) {e.printstacktrace (); } } }}
Get City name/address by latitude (no three-party package required)