Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3428072
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: Java

2010-11-16 11:36:51

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

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

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

  1. "1.0" encoding="utf-8"?>  
  2. ""  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     android:id="@+id/longitude"   
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="longitude:"  
  11.     />  
  12.     android:id="@+id/latitude"    
  13.     android:layout_width="fill_parent"   
  14.     android:layout_height="wrap_content"   
  15.     android:text="latitude:"  
  16.     />  
  17.     android:id="@+id/address"    
  18.     android:layout_width="fill_parent"   
  19.     android:layout_height="wrap_content"   
  20.     />  
  21.   

第三步:修改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.      
  15.     private TextView longitude;  
  16.     private TextView latitude;  
  17.     private TextView address;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.           
  23.         longitude = (TextView)findViewById(R.id.longitude);  
  24.         latitude = (TextView)findViewById(R.id.latitude);  
  25.         address = (TextView)findViewById(R.id.address);  
  26.           
  27.         Location mLocation = getLocation(this);  
  28.         GeoPoint gp = getGeoByLocation(mLocation);  
  29.         Address mAddress = getAddressbyGeoPoint(this, gp);  
  30.       
  31.           
  32.           
  33.         longitude.setText("Longitude: " + mLocation.getLongitude());  
  34.         latitude.setText("Latitude: " + mLocation.getLatitude());  
  35.         address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());  
  36.     }  
  37.       
  38.     //Get the Location by GPS or WIFI  
  39.     public Location getLocation(Context context) {  
  40.         LocationManager locMan = (LocationManager) context  
  41.                 .getSystemService(Context.LOCATION_SERVICE);  
  42.         Location location = locMan  
  43.                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  44.         if (location == null) {  
  45.             location = locMan  
  46.                     .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
  47.         }  
  48.         return location;  
  49.     }  
  50.     //通过Location获取GeoPoint  
  51.      public  GeoPoint getGeoByLocation(Location location) {  
  52.          GeoPoint gp = null;  
  53.          try {  
  54.              if (location != null) {  
  55.                  double geoLatitude = location.getLatitude() * 1E6;  
  56.                  double geoLongitude = location.getLongitude() * 1E6;  
  57.                  gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);  
  58.              }  
  59.          } catch (Exception e) {  
  60.              e.printStackTrace();  
  61.          }  
  62.          return gp;  
  63.      }  
  64.      //通过GeoPoint来获取Address  
  65.      public  Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {  
  66.          Address result = null;  
  67.          try {  
  68.              if (gp != null) {  
  69.                  Geocoder gc = new Geocoder(cntext, Locale.CHINA);  
  70.                   
  71.                  double geoLatitude = (int) gp.getLatitudeE6() / 1E6;  
  72.                  double geoLongitude = (int) gp.getLongitudeE6() / 1E6;  
  73.                    
  74.                  List
     lstAddress = gc.getFromLocation(geoLatitude,  
  75.                          geoLongitude, 1);  
  76.                  if (lstAddress.size() > 0) {  
  77.                      result = lstAddress.get(0);  
  78.                  }  
  79.              }  
  80.          } catch (Exception e) {  
  81.              e.printStackTrace();  
  82.          }  
  83.          return result;  
  84.      }  
  85. }  

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

  1. xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns: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>   

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

阅读(736) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~