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),代码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/longitude"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="longitude:"
- />
- <TextView
- android:id="@+id/latitude"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="latitude:"
- />
- <TextView
- android:id="@+id/address"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
第三步:修改LocationDemo.java(增加了两个方法)代码如下:
- packagecom.android.tutor;
- importjava.util.List;
- importjava.util.Locale;
- importcom.google.android.maps.GeoPoint;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.location.Address;
- importandroid.location.Geocoder;
- importandroid.location.Location;
- importandroid.location.LocationManager;
- importandroid.os.Bundle;
- importandroid.widget.TextView;
- publicclassLocationDemoextendsActivity{
- privateTextViewlongitude;
- privateTextViewlatitude;
- privateTextViewaddress;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- longitude=(TextView)findViewById(R.id.longitude);
- latitude=(TextView)findViewById(R.id.latitude);
- address=(TextView)findViewById(R.id.address);
- LocationmLocation=getLocation(this);
- GeoPointgp=getGeoByLocation(mLocation);
- AddressmAddress=getAddressbyGeoPoint(this,gp);
- longitude.setText("Longitude:"+mLocation.getLongitude());
- latitude.setText("Latitude:"+mLocation.getLatitude());
- address.setText("Address:"+mAddress.getCountryName()+","+mAddress.getLocality());
- }
- //GettheLocationbyGPSorWIFI
- publicLocationgetLocation(Contextcontext){
- LocationManagerlocMan=(LocationManager)context
- .getSystemService(Context.LOCATION_SERVICE);
- Locationlocation=locMan
- .getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if(location==null){
- location=locMan
- .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- }
- returnlocation;
- }
- //通过Location获取GeoPoint
- publicGeoPointgetGeoByLocation(Locationlocation){
- GeoPointgp=null;
- try{
- if(location!=null){
- doublegeoLatitude=location.getLatitude()*1E6;
- doublegeoLongitude=location.getLongitude()*1E6;
- gp=newGeoPoint((int)geoLatitude,(int)geoLongitude);
- }
- }catch(Exceptione){
- e.printStackTrace();
- }
- returngp;
- }
- //通过GeoPoint来获取Address
- publicAddressgetAddressbyGeoPoint(Contextcntext,GeoPointgp){
- Addressresult=null;
- try{
- if(gp!=null){
- Geocodergc=newGeocoder(cntext,Locale.CHINA);
- doublegeoLatitude=(int)gp.getLatitudeE6()/1E6;
- doublegeoLongitude=(int)gp.getLongitudeE6()/1E6;
- List<Address>lstAddress=gc.getFromLocation(geoLatitude,
- geoLongitude,1);
- if(lstAddress.size()>0){
- result=lstAddress.get(0);
- }
- }
- }catch(Exceptione){
- e.printStackTrace();
- }
- returnresult;
- }
- }
第四步:最重要一步在AndroidManiefest.xml中导入Google Api(第14行代码)库,代码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.tutor"
- android:versionCode="1"
- android:versionName="1.0">
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".LocationDemo"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <uses-libraryandroid:name="com.google.android.maps"/>
- </application>
- <uses-sdkandroid:minSdkVersion="7"/>
- <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
- </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),代码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/longitude"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="longitude:"
- />
- <TextView
- android:id="@+id/latitude"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="latitude:"
- />
- <TextView
- android:id="@+id/address"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
第三步:修改LocationDemo.java(增加了两个方法)代码如下:
- packagecom.android.tutor;
- importjava.util.List;
- importjava.util.Locale;
- importcom.google.android.maps.GeoPoint;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.location.Address;
- importandroid.location.Geocoder;
- importandroid.location.Location;
- importandroid.location.LocationManager;
- importandroid.os.Bundle;
- importandroid.widget.TextView;
- publicclassLocationDemoextendsActivity{
- privateTextViewlongitude;
- privateTextViewlatitude;
- privateTextViewaddress;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- longitude=(TextView)findViewById(R.id.longitude);
- latitude=(TextView)findViewById(R.id.latitude);
- address=(TextView)findViewById(R.id.address);
- LocationmLocation=getLocation(this);
- GeoPointgp=getGeoByLocation(mLocation);
- AddressmAddress=getAddressbyGeoPoint(this,gp);
- longitude.setText("Longitude:"+mLocation.getLongitude());
- latitude.setText("Latitude:"+mLocation.getLatitude());
- address.setText("Address:"+mAddress.getCountryName()+","+mAddress.getLocality());
- }
- //GettheLocationbyGPSorWIFI
- publicLocationgetLocation(Contextcontext){
- LocationManagerlocMan=(LocationManager)context
- .getSystemService(Context.LOCATION_SERVICE);
- Locationlocation=locMan
- .getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if(location==null){
- location=locMan
- .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- }
- returnlocation;
- }
- //通过Location获取GeoPoint
- publicGeoPointgetGeoByLocation(Locationlocation){
- GeoPointgp=null;
- try{
- if(location!=null){
- doublegeoLatitude=location.getLatitude()*1E6;
- doublegeoLongitude=location.getLongitude()*1E6;
- gp=newGeoPoint((int)geoLatitude,(int)geoLongitude);
- }
- }catch(Exceptione){
- e.printStackTrace();
- }
- returngp;
- }
- //通过GeoPoint来获取Address
- publicAddressgetAddressbyGeoPoint(Contextcntext,GeoPointgp){
- Addressresult=null;
- try{
- if(gp!=null){
- Geocodergc=newGeocoder(cntext,Locale.CHINA);
- doublegeoLatitude=(int)gp.getLatitudeE6()/1E6;
- doublegeoLongitude=(int)gp.getLongitudeE6()/1E6;
- List<Address>lstAddress=gc.getFromLocation(geoLatitude,
- geoLongitude,1);
- if(lstAddress.size()>0){
- result=lstAddress.get(0);
- }
- }
- }catch(Exceptione){
- e.printStackTrace();
- }
- returnresult;
- }
- }
第四步:最重要一步在AndroidManiefest.xml中导入Google Api(第14行代码)库,代码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.tutor"
- android:versionCode="1"
- android:versionName="1.0">
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".LocationDemo"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <uses-libraryandroid:name="com.google.android.maps"/>
- </application>
- <uses-sdkandroid:minSdkVersion="7"/>
- <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
- </manifest>
第五步:运行上述工程,效果如下图如示:
OK,今天就到这里,如果有什么不明白的,或者想要源代码的,请留下问题或者邮箱。Thx~