Android uses GPS Positioning

Source: Internet
Author: User

1. Code

Package android. test; <br/> Import Java. io. ioexception; <br/> Import Java. util. list; <br/> Import Java. util. locale; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. location. address; <br/> Import android. location. criteria; <br/> Import android. location. geocoder; <br/> Import android. location. location; <br/> Import android. location. locationlistener; <br/> Import and Roid. location. locationmanager; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. widget. textview; <br/> Import android. widget. toast; <br/> Import COM. google. android. maps. geopoint; <br/> public class main extends activity {<br/> private textview m_textview1; <br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstan Cestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> m_textview1 = (textview) findviewbyid (R. id. mytextview1); <br/> locationmanager = (locationmanager) getsystemservice (context. location_service); <br/> Criteria c = new criteria (); <br/> C. setaccuracy (criteria. accuracy_coarse); <br/> C. setaltituderequired (true); <br/> C. setbearingrequired (true); <br /> C. setcostallowed (true); <br/> C. setpowerrequirement (criteria. power_medium); <br/> string strlocation = locationmanager. getbestprovider (C, false); <br/> locationmanager. requestlocationupdates (strlocation, 1000, 0, locationlistener); <br/>}< br/> private final locationlistener = new locationlistener () {<br/> Public void onlocationchanged (location) {// This function is triggered when the coordinates change. If the provider If it is passed to the same coordinates, it will not be triggered <br/> // log it when the location changes <br/> If (location! = NULL) {<br/> log. I ("Supermap", "location changed: Lat:" + location. getlatitude () + "LNG:" <br/> + location. getlongpolling (); <br/> // m_curgeopoint = getgeobylocation (location); <br/> // m_textview1.settext (getaddressbygeopoint (m_curgeopoint); <br/> // toast. maketext (main. this, <br/> // getaddressbygeopoint (m_curgeopoint), 100); <br/> string STR = "Supermap, location changed: Lat:" + location. get Latitude () + "LNG:" <br/> + location. getlongpolling (); <br/> m_textview1.settext (STR); <br/>}< br/> Public void onproviderdisabled (string provider) {<br/> // This function is triggered when the provider is disable. For example, if GPS is disabled, <br/>}< br/> Public void onproviderenabled (string provider) {<br/> // This function is triggered when the provider is enabled. For example, if GPS is enabled, <br/>}< br/> Public void onstatuschanged (string provider, int status, bundle extras) {<br/> // The provider's transition state is in This function is triggered when three statuses are available, temporarily unavailable, and no service are directly switched <br/>}< br/>}; <br/> private string getaddressbygeopoint (geopoint point) {<br/> string STR = ""; <br/> try {<br/> If (point! = NULL) {<br/> geocoder coder = new geocoder (main. this, locale. getdefault (); <br/> double dgeolatitude = (INT) Point. getlatitudee6 ()/1e6; <br/> double dgeolongdistance = (INT) Point. getlongitudee6 ()/1e6; <br/> List <address> addresses = coder. getfromlocation (dgeolatses, dgeolongitude, 1); <br/> stringbuilder sb = new stringbuilder (); <br/> If (addresses. size ()> 0) {<br/> address ADDR = addresses. get (0); <br/> for (INT I = 0; I <addresses. size (); ++ I) {<br/> Sb. append (ADDR. getaddressline (I )). append ("/N"); <br/>}< br/> Sb. append (ADDR. getlocality ()). append ("/N"); <br/> Sb. append (ADDR. getpostalcode ()). append ("/N"); <br/> Sb. append (ADDR. getcountryname ()). append ("/N"); <br/> STR = sb. tostring (); <br/>}< br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> return STR; <br/>}< br/> Private geopoint getgeobylocation (location) {<br/> geopoint point = NULL; <br/> If (location! = NULL) {<br/> double dgeolatitude = location. getlatitude () * 1e6; <br/> double dgeolongdistance = location. getlongpolling () * 1e6; <br/> point = new geopoint (INT) dgeolatitude, (INT) dgeolongpolling); <br/>}< br/> return point; <br/>}< br/>}

 

2. androidmanifest. xml

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "android. test "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <uses-SDK Android: minsdkversion = "7"/> <br/> <application Android: icon = "@ drawable/icon" Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". main "<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/> </Application> <br/> <uses-SDK Android: minsdkversion = "7"/> <br/> <uses-Permission Android: Name = "android. permission. access_fine_location "/> <br/> </manifest>

 

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.