现在的位置: 首页 > 综合 > 正文

通过Location获取Address的使用

2018年04月04日 ⁄ 综合 ⁄ 共 5458字 ⁄ 字号 评论关闭

我们获取Location的目的之一肯定是有获取这个位置的详细地址,而我们有了Location在来获取Address就相对简单多了,因为GoogleApi

已经封装好了方法,我们只需呀通过Location获取GeoPoint,然后在通过GeoPoint来获取我们想要的Address.下面是我做的一个简单的Demo. 


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

 

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

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

  

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

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

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

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

抱歉!评论已关闭.