`
baobaoupup
  • 浏览: 471831 次
文章分类
社区版块
存档分类
最新评论

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. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. packagecom.android.tutor;
  2. importjava.util.List;
  3. importjava.util.Locale;
  4. importcom.google.android.maps.GeoPoint;
  5. importandroid.app.Activity;
  6. importandroid.content.Context;
  7. importandroid.location.Address;
  8. importandroid.location.Geocoder;
  9. importandroid.location.Location;
  10. importandroid.location.LocationManager;
  11. importandroid.os.Bundle;
  12. importandroid.widget.TextView;
  13. publicclassLocationDemoextendsActivity{
  14. privateTextViewlongitude;
  15. privateTextViewlatitude;
  16. privateTextViewaddress;
  17. @Override
  18. publicvoidonCreate(BundlesavedInstanceState){
  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. LocationmLocation=getLocation(this);
  25. GeoPointgp=getGeoByLocation(mLocation);
  26. AddressmAddress=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. //GettheLocationbyGPSorWIFI
  32. publicLocationgetLocation(Contextcontext){
  33. LocationManagerlocMan=(LocationManager)context
  34. .getSystemService(Context.LOCATION_SERVICE);
  35. Locationlocation=locMan
  36. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  37. if(location==null){
  38. location=locMan
  39. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  40. }
  41. returnlocation;
  42. }
  43. //通过Location获取GeoPoint
  44. publicGeoPointgetGeoByLocation(Locationlocation){
  45. GeoPointgp=null;
  46. try{
  47. if(location!=null){
  48. doublegeoLatitude=location.getLatitude()*1E6;
  49. doublegeoLongitude=location.getLongitude()*1E6;
  50. gp=newGeoPoint((int)geoLatitude,(int)geoLongitude);
  51. }
  52. }catch(Exceptione){
  53. e.printStackTrace();
  54. }
  55. returngp;
  56. }
  57. //通过GeoPoint来获取Address
  58. publicAddressgetAddressbyGeoPoint(Contextcntext,GeoPointgp){
  59. Addressresult=null;
  60. try{
  61. if(gp!=null){
  62. Geocodergc=newGeocoder(cntext,Locale.CHINA);
  63. doublegeoLatitude=(int)gp.getLatitudeE6()/1E6;
  64. doublegeoLongitude=(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(Exceptione){
  72. e.printStackTrace();
  73. }
  74. returnresult;
  75. }
  76. }

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

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

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

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


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

Android高手进阶教程(十三)---Android Location的使用!

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

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

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

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. packagecom.android.tutor;
  2. importjava.util.List;
  3. importjava.util.Locale;
  4. importcom.google.android.maps.GeoPoint;
  5. importandroid.app.Activity;
  6. importandroid.content.Context;
  7. importandroid.location.Address;
  8. importandroid.location.Geocoder;
  9. importandroid.location.Location;
  10. importandroid.location.LocationManager;
  11. importandroid.os.Bundle;
  12. importandroid.widget.TextView;
  13. publicclassLocationDemoextendsActivity{
  14. privateTextViewlongitude;
  15. privateTextViewlatitude;
  16. privateTextViewaddress;
  17. @Override
  18. publicvoidonCreate(BundlesavedInstanceState){
  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. LocationmLocation=getLocation(this);
  25. GeoPointgp=getGeoByLocation(mLocation);
  26. AddressmAddress=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. //GettheLocationbyGPSorWIFI
  32. publicLocationgetLocation(Contextcontext){
  33. LocationManagerlocMan=(LocationManager)context
  34. .getSystemService(Context.LOCATION_SERVICE);
  35. Locationlocation=locMan
  36. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  37. if(location==null){
  38. location=locMan
  39. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  40. }
  41. returnlocation;
  42. }
  43. //通过Location获取GeoPoint
  44. publicGeoPointgetGeoByLocation(Locationlocation){
  45. GeoPointgp=null;
  46. try{
  47. if(location!=null){
  48. doublegeoLatitude=location.getLatitude()*1E6;
  49. doublegeoLongitude=location.getLongitude()*1E6;
  50. gp=newGeoPoint((int)geoLatitude,(int)geoLongitude);
  51. }
  52. }catch(Exceptione){
  53. e.printStackTrace();
  54. }
  55. returngp;
  56. }
  57. //通过GeoPoint来获取Address
  58. publicAddressgetAddressbyGeoPoint(Contextcntext,GeoPointgp){
  59. Addressresult=null;
  60. try{
  61. if(gp!=null){
  62. Geocodergc=newGeocoder(cntext,Locale.CHINA);
  63. doublegeoLatitude=(int)gp.getLatitudeE6()/1E6;
  64. doublegeoLongitude=(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(Exceptione){
  72. e.printStackTrace();
  73. }
  74. returnresult;
  75. }
  76. }

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

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

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

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics