Android高手進階教程(十五)之—通過Location擷取Address的使用!

來源:互聯網
上載者:User

大家好,上一節我講了一下如何通過LocationManager來擷取Location,沒有看過上一節的同學,可以點擊如下連結返回查看:

Android高手進階教程十四之---Android Location的使用!

我們擷取Location的目的之一肯定是有擷取這個位置的詳細地址,而我們有了Location在來擷取Address就相對簡單多了,因為GoogleApi已經封裝好了方法,我們只需呀通過Location擷取GeoPoint,然後在通過GeoPoint來擷取我們想要的Address.下面是我做的一個簡單的Demo.

 

第一步建立一個Android工程LocationDemo,注意這裡選用的是(Google APIs),下面是檔案目錄結構:

 

第二步: 修改main.xml(相比第十四節增加了一個address的TextView),代碼如下:

<?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 /><TextView<br />android:id="@+id/address"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> /><br /></LinearLayout>

第三步:修改LocationDemo.java(增加了兩個方法)代碼如下:

package com.android.tutor;<br />import java.util.List;<br />import java.util.Locale;<br />import com.google.android.maps.GeoPoint;<br />import android.app.Activity;<br />import android.content.Context;<br />import android.location.Address;<br />import android.location.Geocoder;<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 />private TextView address;<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);<br /> address = (TextView)findViewById(R.id.address);</p><p> Location mLocation = getLocation(this);<br /> GeoPoint gp = getGeoByLocation(mLocation);<br /> Address mAddress = getAddressbyGeoPoint(this, gp);</p><p> longitude.setText("Longitude: " + mLocation.getLongitude());<br /> latitude.setText("Latitude: " + mLocation.getLatitude());<br /> address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());<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 />//通過Location擷取GeoPoint<br /> public GeoPoint getGeoByLocation(Location location) {<br /> GeoPoint gp = null;<br /> try {<br /> if (location != null) {<br /> double geoLatitude = location.getLatitude() * 1E6;<br /> double geoLongitude = location.getLongitude() * 1E6;<br /> gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> return gp;<br /> }<br /> //通過GeoPoint來擷取Address<br /> public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {<br /> Address result = null;<br /> try {<br /> if (gp != null) {<br /> Geocoder gc = new Geocoder(cntext, Locale.CHINA);</p><p> double geoLatitude = (int) gp.getLatitudeE6() / 1E6;<br /> double geoLongitude = (int) gp.getLongitudeE6() / 1E6;</p><p> List<Address> lstAddress = gc.getFromLocation(geoLatitude,<br /> geoLongitude, 1);<br /> if (lstAddress.size() > 0) {<br /> result = lstAddress.get(0);<br /> }<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> return result;<br /> }<br />}

第四步:最重要一步在AndroidManiefest.xml中匯入Google Api(第14行代碼)庫,代碼如下:

<?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 /> <uses-library android:name="com.google.android.maps" /><br /> </application><br /> <uses-sdk android:minSdkVersion="7" /><br /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><br /></manifest>

 

第五步:運行上述工程,效果如如示:

 

OK,今天就到這裡,如果有什麼不明白的,或者想要原始碼的,請留下問題或者郵箱。Thx~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.