Android advanced tutorial (15th)-use location to get address!

Source: Internet
Author: User

Hello everyone, I spoke about how to get location via locationmanager in the previous section. If you have not read the previous section, you can click the following link to view it:

Android advanced tutorial 14 --- use of Android location!

One of our purposes to get location is to get the detailed address of this location, and we have location to get the address, which is much easier, because googleapi has encapsulated the method, we only need to get geopoint through location, and then get the address we want through geopoint. the following is a simple demo.

 

Step 1 create an android project locationdemo. Note that Google APIS is used here. The following is the file directory structure:

 

Step 2: modify main. XML (a address textview is added in section 14th). The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: id = "@ + ID/longpolling" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "longpolling:" <br/> <textview <br/> Android: Id = "@ + ID/latitude" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "latitude: "<br/> <textview <br/> Android: Id =" @ + ID/address "<br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> </linearlayout>

Step 3: Modify locationdemo. Java (two methods are added). The Code is as follows:

Package COM. android. tutor; <br/> Import Java. util. list; <br/> Import Java. util. locale; <br/> Import COM. google. android. maps. geopoint; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. location. address; <br/> Import android. location. geocoder; <br/> Import android. location. location; <br/> Import android. location. locationmanager; <br/> Import android. OS. bundle; <br/> I Mport android. widget. textview; <br/> public class locationdemo extends activity {</P> <p> private textview longpolling; <br/> private textview latitude; <br/> private textview address; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> longpolling = (textview) findviewbyid (R. id. longpolling); <br/> latitu De = (textview) findviewbyid (R. id. latitude); <br/> address = (textview) findviewbyid (R. id. address); </P> <p> location mlocation = getlocation (this); <br/> geopoint Gp = getgeobylocation (mlocation ); <br/> address maddress = getaddressbygeopoint (this, GP); </P> <p> longpolling. settext ("longpolling:" + mlocation. getlongpolling (); <br/> latitude. settext ("latitude:" + mlocation. getlatitude (); <br/> address. set Text ("Address:" + maddress. getcountryname () + "," + maddress. getlocality (); <br/>}</P> <p> // get the location by GPS or WiFi <br/> Public location getlocation (context) {<br/> locationmanager locman = (locationmanager) Context <br/>. getsystemservice (context. location_service); <br/> location = locman <br/>. getlastknownlocation (locationmanager. gps_provider); <br/> If (location = NULL) {<Br/> location = locman <br/>. getlastknownlocation (locationmanager. network_provider); <br/>}< br/> return location; <br/>}< br/> // obtain geopoint through location <br/> Public geopoint getgeobylocation (location) {<br/> geopoint Gp = NULL; <br/> try {<br/> If (location! = NULL) {<br/> double geolatitude = location. getlatitude () * 1e6; <br/> double geolongdistance = location. getlongpolling () * 1e6; <br/> Gp = new geopoint (INT) geolatitude, (INT) geolongpolling ); <br/>}< br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return gp; <br/>}< br/> // obtain address through geopoint <br/> Public Address getaddressbygeopoint (context cntext, geopoint GP) {<br/> addh SS result = NULL; <br/> try {<br/> If (GP! = NULL) {<br/> geocoder GC = new geocoder (cntext, locale. china); </P> <p> double geolatitude = (INT) GP. getlatitudee6 ()/1e6; <br/> double geolong1_= (INT) GP. getlongitudee6 ()/1e6; </P> <p> List <address> lstaddress = GC. getfromlocation (geolatitude, <br/> geolongdistance, 1); <br/> If (lstaddress. size ()> 0) {<br/> result = lstaddress. get (0); <br/>}< br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return result; <br/>}< br/>}

Step 4: import the Google API (14th lines of code) library to androidmaniefest. xml. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. android. tutor "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". locationdemo "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <uses-library Android: Name =" com. google. android. maps "/> <br/> </Application> <br/> <uses-SDK Android: minsdkversion =" 7 "/> <br/> <uses-Permission Android: name = "android. permission. access_fine_location "/> <br/> </manifest>

 

Step 5: run the above project, as shown in the following figure:

 

OK. Now, if you do not understand anything or want the source code, leave a question or email address. THX ~

Related Article

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.