大家好,今天說說Location
, Location
在Android
開發中還是經常用到的,比如 通過經緯度擷取天氣,根據Location
擷取所在地區詳細Address
(比如Google Map
開發).等。而在Android
中通過LocationManager
來擷取Location
.通常擷取Location
有GPS
擷取,WIFI
擷取。
我今天做一個簡單的小Demo
,來教大家如何擷取Location
,從而擷取經緯度。下一節將教大家通過Location
來擷取Address
.
首先第一步:
建立一個Android
工程命名為LocationDemo
.
第二步:修改main.xml
代碼如下:
<?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 /> ><br /><TextView<br />android:id="@+id/longitude"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="longitude:"<br /> /><br /><TextView<br />android:id="@+id/latitude"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="latitude:"<br /> /><br /></LinearLayout><br />
第三步:修改LocationDemo.java
,代碼如下:
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 longitude;<br />private TextView latitude;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> longitude = (TextView)findViewById(R.id.longitude);<br /> latitude = (TextView)findViewById(R.id.latitude);</p><p> Location mLocation = getLocation(this);</p><p> longitude.setText("Longitude: " + mLocation.getLongitude());<br /> latitude.setText("Latitude: " + mLocation.getLatitude());<br /> }</p><p> //Get the Location by GPS or WIFI<br />public Location getLocation(Context context) {<br />LocationManager locMan = (LocationManager) context<br />.getSystemService(Context.LOCATION_SERVICE);<br />Location 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 />}
第四步:增加許可權,修改AndroidManifest.xml
代碼如下(第16行為所增行):
<?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>
第五步:運行LocationDemo
工程,所得效果如下(真機深圳測試):