Obtain the city name/address by latitude and longitude (without a third-party package)
1: The getLocation () method filters out the optimal method for obtaining the longitude and latitude.
2: The MapThread uploads the longitude and latitude obtained by getLocation () to obtain the city name.
Public class PositionActivity extends BaseActivity implements IInit, ireeclipsehandler, View. onClickListener {private TextView mLocationTV, mCategoryTV; // location. type private double latitude, longlatitude; // longitude and 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 () {mLocatio NTV = (TextView) findViewById (R. id. TV _goods_location);, getLocation (); // obtain the latitude and longitude mapThread = new MapThread (); mapThread. start () ;}// obtain the latitude and longitude public void getLocation () {LocationManager lm = (LocationManager) getSystemService (Context. LOCATION_SERVICE); List <String> lp = lm. getAllProviders (); // returns the name list of all known location providers, including those that are not allowed to access or that are currently being called. For (String item: lp) {// Log. I ("----> available location service:", item);} Criteria criteria = new Criteria (); criteria. setCostAllowed (false); // set the location service to criteria for free. setAccuracy (Criteria. ACCURACY_COARSE); // sets the horizontal position precision String providerName = lm. getBestProvider (criteria, true); // getBestProvider will be returned if (providerName! = Null) {Location location = lm. getLastKnownLocation (providerName); latitude = location. getLatitude (); // obtain the dimension information longpolling = location. getlongpolling (); // obtain the longitude information} else {ToastUtil. show (this, "1. check network connection \ n2. open my location ") ;}} class MapThread extends Thread {@ Override public void run () {super. run (); String uriStr = MessageFormat. format (mapUriStr, latitude, longpolling); HttpGet httpGet = new HttpGet (uriStr ); // Generate a request object HttpClient httpClient = new DefaultHttpClient (); // generate an Http client object try {httpResponse = httpClient.exe cute (httpGet ); // use the Http client to send the request object httpEntity = httpResponse. getEntity (); // get the 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 from here