Android高手进阶教程(十五)之---通过Location获取Address的使用!

大家好,上一节我讲了一下如何通过LocationManager来获取Location,没有看过上一节的同学,可以点击如下链接返回查看:

Android高手进阶教程十四之---Android Location的使用!

我们获取Location的目的之一肯定是有获取这个位置的详细地址,而我们有了Location在来获取Address就相对简单多了,因为GoogleApi已经封装好了方法,我们只需呀通过Location获取GeoPoint,然后在通过GeoPoint来获取我们想要的Address.下面是我做的一个简单的Demo.

第一步新建一个Android工程LocationDemo,注意这里选用的是(Google APIs),下面是文件目录结构:

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:id="@+id/longitude"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="longitude:"
  12. />
  13. <TextView
  14. android:id="@+id/latitude"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="latitude:"
  18. />
  19. <TextView
  20. android:id="@+id/address"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. />
  24. </LinearLayout>

第三步:修改LocationDemo.java(增加了两个方法)代码如下:

  1. package com.android.tutor;
  2. import java.util.List;
  3. import java.util.Locale;
  4. import com.google.android.maps.GeoPoint;
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.location.Address;
  8. import android.location.Geocoder;
  9. import android.location.Location;
  10. import android.location.LocationManager;
  11. import android.os.Bundle;
  12. import android.widget.TextView;
  13. public class LocationDemo extends Activity {
  14. private TextView longitude;
  15. private TextView latitude;
  16. private TextView address;
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. longitude = (TextView)findViewById(R.id.longitude);
  22. latitude = (TextView)findViewById(R.id.latitude);
  23. address = (TextView)findViewById(R.id.address);
  24. Location mLocation = getLocation(this);
  25. GeoPoint gp = getGeoByLocation(mLocation);
  26. Address mAddress = getAddressbyGeoPoint(this, gp);
  27. longitude.setText("Longitude: " + mLocation.getLongitude());
  28. latitude.setText("Latitude: " + mLocation.getLatitude());
  29. address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());
  30. }
  31. //Get the Location by GPS or WIFI
  32. public Location getLocation(Context context) {
  33. LocationManager locMan = (LocationManager) context
  34. .getSystemService(Context.LOCATION_SERVICE);
  35. Location location = locMan
  36. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  37. if (location == null) {
  38. location = locMan
  39. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  40. }
  41. return location;
  42. }
  43. //通过Location获取GeoPoint
  44. public GeoPoint getGeoByLocation(Location location) {
  45. GeoPoint gp = null;
  46. try {
  47. if (location != null) {
  48. double geoLatitude = location.getLatitude() * 1E6;
  49. double geoLongitude = location.getLongitude() * 1E6;
  50. gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
  51. }
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. return gp;
  56. }
  57. //通过GeoPoint来获取Address
  58. public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {
  59. Address result = null;
  60. try {
  61. if (gp != null) {
  62. Geocoder gc = new Geocoder(cntext, Locale.CHINA);
  63. double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
  64. double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
  65. List<Address> lstAddress = gc.getFromLocation(geoLatitude,
  66. geoLongitude, 1);
  67. if (lstAddress.size() > 0) {
  68. result = lstAddress.get(0);
  69. }
  70. }
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. return result;
  75. }
  76. }

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.tutor"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".LocationDemo"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <uses-library android:name="com.google.android.maps" />
  15. </application>
  16. <uses-sdk android:minSdkVersion="7" />
  17. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  18. </manifest>

第五步:运行上述工程,效果如下图如示:

OK,今天就到这里,如果有什么不明白的,或者想要源代码的,请留下问题或者邮箱。Thx~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值