Android Geocoder (location resolution)

Source: Internet
Author: User

The GPS location service is available in Android, and the developer can parse the location information to get the location details.

1.gps positioning

Build an Android app in Eclipse. The Android SDK provides locationmanager to get the location services provided by the system, which can be located by GPS, network, etc. The location object can be obtained through the following code, and latitude and longitude information is obtained through location.

1Locationmanager loactionmanager=(Locationmanager) Getsystemservice (context.location_service);2tv_msg=(TextView) Findviewbyid (r.id.textview1);3Criteria criteria=NewCriteria ();4 criteria.setaccuracy (criteria.accuracy_fine);5Criteria.setaltituderequired (false);6Criteria.setbearingrequired (false);7Criteria.setcostallowed (false);8 criteria.setpowerrequirement (criteria.power_low);9String Provider=loactionmanager.getbestprovider (Criteria,true);TenLocation Location=loactionmanager.getlastknownlocation (provider);
Get Service

Specify some parameters for the object by criteria. Criteria provides several methods:

(1) public void setaccuracy (int accuracy)

The precision of the positional resolution. Criteria.accuracy_fine precision mode, criteria.accuracy_coarse blur mode.

(2) public void setaltituderequired (Boolean altitude)

Whether to provide altitude information

(3) public void setbearingrequired (Boolean)

Whether to provide direction information

(4) public void SetPowerRequirement (Int. level)

Battery consumption, no, low, medium, high, parameters Criteria. No_requirement, Criteria. Power_low, Criteria.power_medium, or Criteria. Power_high

(5) public void setcostallowed (Boolean)

Whether the operator is allowed to charge.

2.gps Position Resolution

Before the location of the resolution, someone to visit the services provided by Google for location resolution, that is really time-consuming, for mobile phone users, experience is really important, today inadvertently see can be another way to resolve the location. The Geocoder class is available in the Android SDK for location resolution. The address list is obtained through Geocoder's getfromlocation (double lattitude,double longtitude,int maxresults), with three parameters: latitude, longitude, number of results returned. At the same time, the Getlocationfrom method of Geocoder is synchronous, so they will block the thread when they find it, so it should not be put into the UI thread, it should be in the background, that is, by asynchronous thread.

1  Public classGeocodeaddressextendsAsynctask<location, Void, string>2     {3         4 @Override5         protected voidOnPreExecute () {6             //TODO auto-generated Method Stub7             Super. OnPreExecute ();8         }9 @OverrideTen         protectedString doinbackground (location ... params) { One             //TODO auto-generated Method Stub A             if(params[0]!=NULL) -             { -Geocoder geocoder=NewGeocoder (mainactivity. This); the                 Try { -List<address> address=geocoder.getfromlocation (Params[0].getlatitude (), Params[0].getlongitude (), 1); -Msg= ""; -                     if(Address.size () >0) +                     { -msg+= "Longitude:" +string.valueof (Address.get (0). Getlongitude () *1e6) + "\ n"; +msg+= "Latitude:" +string.valueof (Address.get (0). Getlatitude () *1e6) + "\ n"; Amsg+= "Country:" +address.get (0). Getcountryname () + "\ n"; atmsg+= "Province:" +address.get (0). Getadminarea () + "\ n"; -msg+= "City:" +address.get (0). Getlocality () + "\ n"; -msg+= "Name:" +address.get (0). Getaddressline (1) + "\ n"; -msg+= "Street:" +address.get (0). Getaddressline (0); -                     returnmsg; -                     } in}Catch(IOException e) { -                     //TODO auto-generated Catch block to e.printstacktrace (); +                 } -             } the             return NULL; *         } $ @OverridePanax Notoginseng         protected voidOnPostExecute (String result) { -             //TODO auto-generated Method Stub the             if(result!=NULL&&result!= "") +             { A Tv_msg.settext (result); the             } +         } -          $}
Location Resolution

The geoceaddress here is the inherited Asynctask asynchronous class, with parameters such as location, the type of parameter passed in, Void, the progress of the asynchronous thread processing, String, and the returned result type.

Doinbackground (Params): Used to handle more time-consuming operations, returning results

OnPostExecute: Gets the results of the processing and can be updated with the UI.

Onprogressupdate: Progress of data processing

3. Position Reverse Resolution

Position reverse resolution, refers to the location of information to obtain the latitude and longitude address.

List<address> geoaddress= geocoder.getfromlocationname (String locationname, int maxresult);

Parameters: LocationName: Location of the name information, such as Nanjing University, Beijing University and so on.

Maxresults: The number of results returned. From 1 to 5.

Double Lattitude=geoaddress.get (0). Getlatitude ();//Get Latitude
Double Longtitude=geoaddress.get (0). Getlongitude ();//Get Longitude

For more details, please refer to the ANDROIDSDK documentation.

Android Geocoder (location resolution)

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.