Hello everyone, let's talk about it today.Location
,Location
InAndroid
It is often used during development. For example, you can obtain the weather by longitude and latitude.Location
Obtain region detailsAddress
(For exampleGoogle Map
Development. InAndroid
PassedLocationmanager
To obtainLocation
. Usually getLocation
YesGPS
Get,WiFi
.
I am doing a simple littleDemo
To teach you how to obtainLocation
To obtain the longitude and latitude. The next section will teach you how to useLocation
To obtainAddress
.
First step:
CreateAndroid
Project name:Locationdemo
.
Step 2: ModifyMain. xml
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/> </linearlayout> <br/>
Step 3: ModifyLocationdemo. Java
The Code is as follows:
Package COM. android. tutor; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. location. location; <br/> Import android. location. locationmanager; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; <br/> public class locationdemo extends activity {</P> <p> private textview longpolling; <br/> private textview latitude; <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/> latitude = (textview) findviewbyid (R. id. latitude); </P> <p> location mlocation = getlocation (this); </P> <p> longpolling. settext ("longpolling:" + mlocation. getlongpolling (); <br/> latitude. settext ("latitude:" + mlocation. getlatitude (); <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/>}
Step 4: Add and modify permissionsAndroidmanifest. xml
The Code is as follows (16th rows added by the Action ):
<? 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/> </Application> <br/> <uses-SDK Android: minsdkversion = "7"/> <br/> <uses-Permission Android: Name = "android. permission. access_fine_location "/> <br/> </manifest>
Step 5: RunLocationdemo
Project ):