大家好,上一節我講了一下如何通過LocationManager來獲取Location,沒有看過上一節的同學,可以點擊如下鏈接返回查看:
[**Android高手進階教程十四之---Android Location的使用!**](http://blog.csdn.net/Android_Tutor/archive/2010/06/15/5672911.aspx)
我們獲取Location的目的之一肯定是有獲取這個位置的詳細地址,而我們有了Location在來獲取Address就相對簡單多了,因為GoogleApi已經封裝好了方法,我們只需呀通過Location獲取GeoPoint,然后在通過GeoPoint來獲取我們想要的Address.下面是我做的一個簡單的Demo.
?
第一步新建一個Android工程LocationDemo,注意這里選用的是(Google APIs),下面是文件目錄結構:

?
第二步: 修改main.xml(相比第十四節增加了一個address的TextView),代碼如下:
~~~
<?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:" /><TextView android:id="@+id/address" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
~~~
第三步:修改LocationDemo.java(增加了兩個方法)代碼如下:
~~~
package com.android.tutor;import java.util.List;import java.util.Locale;import com.google.android.maps.GeoPoint;import android.app.Activity;import android.content.Context;import android.location.Address;import android.location.Geocoder;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; private TextView address; @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); address = (TextView)findViewById(R.id.address); Location mLocation = getLocation(this); GeoPoint gp = getGeoByLocation(mLocation); Address mAddress = getAddressbyGeoPoint(this, gp); longitude.setText("Longitude: " + mLocation.getLongitude()); latitude.setText("Latitude: " + mLocation.getLatitude()); address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality()); } //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; } //通過Location獲取GeoPoint public GeoPoint getGeoByLocation(Location location) { GeoPoint gp = null; try { if (location != null) { double geoLatitude = location.getLatitude() * 1E6; double geoLongitude = location.getLongitude() * 1E6; gp = new GeoPoint((int) geoLatitude, (int) geoLongitude); } } catch (Exception e) { e.printStackTrace(); } return gp; } //通過GeoPoint來獲取Address public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) { Address result = null; try { if (gp != null) { Geocoder gc = new Geocoder(cntext, Locale.CHINA); double geoLatitude = (int) gp.getLatitudeE6() / 1E6; double geoLongitude = (int) gp.getLongitudeE6() / 1E6; List<Address> lstAddress = gc.getFromLocation(geoLatitude, geoLongitude, 1); if (lstAddress.size() > 0) { result = lstAddress.get(0); } } } catch (Exception e) { e.printStackTrace(); } return result; }}
~~~
第四步:最重要一步在AndroidManiefest.xml中導入Google Api(第14行代碼)庫,代碼如下:
~~~
<?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> <uses-library android:name="com.google.android.maps" /> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/></manifest>
~~~
?
第五步:運行上述工程,效果如下圖如示:

?
OK,今天就到這里,如果有什么不明白的,或者想要源代碼的,請留下問題或者郵箱。Thx~
- 前言
- (一)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!!!