Obtain the city name/address by latitude and longitude (without a third-party package)

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.