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

简介: 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),代码如下:

view plaincopy to clipboardprint?

<?xml version="1.0" encoding="utf-8"?>   
<LinearLayout xmlns:android="https://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(增加了两个方法)代码如下:

view plaincopy to clipboardprint?

package com.android.tutor;   
import java.util.List;   
import java.util.Locale;   
import com.google.android.maps.GeoPoint;   
import android.app.Activity;   
import android.content.Context;   
import android.location.Address;   
import android.location.Geocoder;   
import android.location.Location;   
import android.location.LocationManager;   
import android.os.Bundle;   
import android.widget.TextView;   
public class LocationDemo extends Activity {   
      
    private TextView longitude;   
    private TextView latitude;   
    private TextView address;   
    @Override  
    public void onCreate(Bundle savedInstanceState) {   
        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);   
           
        Location mLocation = getLocation(this);   
        GeoPoint gp = getGeoByLocation(mLocation);   
        Address mAddress = getAddressbyGeoPoint(this, gp);   
       
           
           
        longitude.setText("Longitude: " + mLocation.getLongitude());   
        latitude.setText("Latitude: " + mLocation.getLatitude());   
        address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());   
    }   
       
    //Get the Location by GPS or WIFI   
    public Location getLocation(Context context) {   
        LocationManager locMan = (LocationManager) context   
                .getSystemService(Context.LOCATION_SERVICE);   
        Location location = locMan   
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);   
        if (location == null) {   
            location = locMan   
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);   
        }   
        return location;   
    }   
    //通过Location获取GeoPoint   
     public  GeoPoint getGeoByLocation(Location location) {   
         GeoPoint gp = null;   
         try {   
             if (location != null) {   
                 double geoLatitude = location.getLatitude() * 1E6;   
                 double geoLongitude = location.getLongitude() * 1E6;   
                 gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);   
             }   
         } catch (Exception e) {   
             e.printStackTrace();   
         }   
         return gp;   
     }   
     //通过GeoPoint来获取Address   
     public  Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {   
         Address result = null;   
         try {   
             if (gp != null) {   
                 Geocoder gc = new Geocoder(cntext, Locale.CHINA);   
                   
                 double geoLatitude = (int) gp.getLatitudeE6() / 1E6;   
                 double geoLongitude = (int) gp.getLongitudeE6() / 1E6;   
                    
                 List<Address> lstAddress = gc.getFromLocation(geoLatitude,   
                         geoLongitude, 1);   
                 if (lstAddress.size() > 0) {   
                     result = lstAddress.get(0);   
                 }   
             }   
         } catch (Exception e) {   
             e.printStackTrace();   
         }   
         return result;   
     }   
}

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

view plaincopy to clipboardprint?

1. <?xml version="1.0" encoding="utf-8"?>  
2. <manifest xmlns:android="https://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>   
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.android.tutor" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".LocationDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="com.google.android.maps" /> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> </manifest>

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

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

相关文章
|
8月前
|
网络协议 Android开发 数据安全/隐私保护
Android手机上使用Socks5全局代理-教程+软件
Android手机上使用Socks5全局代理-教程+软件
5603 2
|
8月前
|
缓存 网络协议 安全
49. 【Android教程】HTTP 使用详解
49. 【Android教程】HTTP 使用详解
142 1
|
3月前
|
Android开发 数据安全/隐私保护 虚拟化
安卓手机远程连接登录Windows服务器教程
安卓手机远程连接登录Windows服务器教程
593 4
|
3月前
|
Android开发
布谷语音软件开发:android端语音软件搭建开发教程
语音软件搭建android端语音软件开发教程!
|
8月前
|
XML 存储 JSON
51. 【Android教程】JSON 数据解析
51. 【Android教程】JSON 数据解析
194 2
|
8月前
|
存储 API 文件存储
47. 【Android教程】SharedPreferences 存储
47. 【Android教程】SharedPreferences 存储
88 2
|
6月前
|
开发工具 Android开发
Flutter: Android SDK not found at this location,Android Studio not found at xxx
Flutter: Android SDK not found at this location,Android Studio not found at xxx
218 2
|
8月前
|
存储 编解码 Android开发
58. 【Android教程】音频录制:MediaRecord
58. 【Android教程】音频录制:MediaRecord
88 2
|
8月前
|
Web App开发 移动开发 前端开发
52. 【Android教程】网页视图:WebView
52. 【Android教程】网页视图:WebView
130 1
|
8月前
|
XML 存储 JavaScript
50. 【Android教程】xml 数据解析
50. 【Android教程】xml 数据解析
93 1

热门文章

最新文章

  • 1
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 2
    Android历史版本与APK文件结构
  • 3
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 4
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
  • 5
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
  • 6
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 7
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 8
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 9
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 10
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
  • 1
    android FragmentManager 删除所有Fragment 重建
    15
  • 2
    Android实战经验之Kotlin中快速实现MVI架构
    21
  • 3
    即时通讯安全篇(一):正确地理解和使用Android端加密算法
    24
  • 4
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
    39
  • 5
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    122
  • 6
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    40
  • 7
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    55
  • 8
    Android历史版本与APK文件结构
    149
  • 9
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    46
  • 10
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    40