Get City name/address by latitude (no three-party package required)

Source: Internet
Author: User

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}&AMP;SENSOR=TRUE&AMP;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)

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.