大家好,今天說說**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"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:id="@+id/longitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="longitude:" /><TextView android:id="@+id/latitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="latitude:" /></LinearLayout>
~~~
?
第三步:修改**LocationDemo.java**,代碼如下:
~~~
package com.android.tutor;import android.app.Activity;import android.content.Context;import android.location.Location;import android.location.LocationManager;import android.os.Bundle;import android.widget.TextView;public class LocationDemo extends Activity { private TextView longitude; private TextView latitude; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); longitude = (TextView)findViewById(R.id.longitude); latitude = (TextView)findViewById(R.id.latitude); Location mLocation = getLocation(this); longitude.setText("Longitude: " + mLocation.getLongitude()); latitude.setText("Latitude: " + mLocation.getLatitude()); } //Get the Location by GPS or WIFI public Location getLocation(Context context) { LocationManager locMan = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); Location location = locMan .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null) { location = locMan .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } return location; }}
~~~
?
第四步:增加權限,修改**AndroidManifest.xml**代碼如下(第16行為所增行):
~~~
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.tutor" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".LocationDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/></manifest>
~~~
?
第五步:運行**LocationDemo**工程,所得效果如下(真機深圳測試):
?

- 前言
- (一)Android常用名令集錦(圖文并茂)!
- (二)Android Launcher抽屜類SlidingDrawer的使用!
- (三)Android 中自定義View的應用.
- (四)Android 中自定義屬性(attr.xml,TypedArray)的使用!
- (五)Android 中LayoutInflater的使用!
- (六)Android 中MenuInflater的使用(布局定義菜單)!
- (七)Android 中Preferences的使用!
- (八)Android Widget開發案例(世界杯倒計時!)
- (九)Android Handler的使用!!!
- (十)Android PopupWindow的使用!!!
- (十一)Android 通用獲取Ip的方法(判斷手機是否聯網的方法)!!!
- (十二)Android 在一個應用中如何啟動另外一個已安裝的應用!!!
- (十三)Android 數據庫SQLiteDatabase的使用!!
- (十四)Android Location的使用!!
- (十五)通過Location獲取Address的使用!
- (十六)Android中萬能的BaseAdapter(Spinner,ListView,GridView)的使用!
- Android 中的拿來主義(編譯,反編譯,AXMLPrinter2,smali,baksmali)!
- (十七)Android中Intent傳遞對象的兩種方法(Serializable,Parcelable)!
- (十八)列出Android設備中所有啟動的服務,及判斷某個服務是否開啟!
- (十九)Android開發中,使用線程應該注意的問題!
- (二十)Android與JavaScript方法相互調用!
- (二十一)Android中創建與幾種解析xml的方法!
- (二十二)Android中幾種圖像特效處理的集錦!!
- (二十三)Android中的日歷讀寫操作!!!
- (二十四)Android WebView的緩存!!!
- (二十五)Android 中的AIDL!!!