首页 > 其他分享 >Android高手进阶教程(十五)之---通过Location获取Address的使用!

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

时间:2023-02-28 11:32:29浏览次数:38  
标签:layout 进阶 GeoPoint --- location Address import android Location


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

​​Android高手进阶教程十四之---Android Location的使用!​​

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

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

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

​​view plain​​​​copy to clipboard​​​​print​​​​?​​
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="https://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(增加了两个方法)代码如下:

​​view plain​​​​copy to clipboard​​​​print​​​​?​​
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<Address> 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行代码)库,代码如下:

​​view plain​​​​copy to clipboard​​​​print​​​​?​​
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,今天就到这里,如果有什么不明白的,或者想要源代码的,请留下问题


标签:layout,进阶,GeoPoint,---,location,Address,import,android,Location
From: https://blog.51cto.com/u_15070324/6090635

相关文章

  • C# 货币金额中文(英文)大写转换方法-工具类
    1publicstaticclassMoney{2privatestaticreadonlyStringcnNumber="零壹贰叁肆伍陆柒捌玖";3privatestaticreadonlyStringcnUni......
  • 基于QT实现的影院票务系统[2023-02-28]
    基于QT实现的影院票务系统[2023-02-28]基于QT实现的影院票务系统影院票务系统1.系统权限管理系统分3种用户权限:A游客权限-注册会员,查看电影场次信息,购买电影票B会......
  • shell-awk-打印进程ID
    dockerps|grepcita|awk-F""'{print$1}'awk-F""按空格分割e.g.[root@host-10-0-169-67~]#sudodockerpsCONTAINERIDIMAGE......
  • LeetCode算法训练-回溯 491.递增子序列 46.全排列 47.全排列 II
    欢迎关注个人公众号:爱喝可可牛奶LeetCode算法训练-回溯491.递增子序列46.全排列47.全排列IILeetCode491.递增子序列分析找出并返回所有数组中不同的递增子序列......
  • 华为LAB实验室3-机器学习实验:(线性回归)美国King County房价预测训练赛
    各位好,我是乾颐堂大堂子。领取完整实战指南可以私信我,关键词:实战指南1.导入相关python库2.数据处理下载的是两个数据文件,一个是真实数据,一个是测试数据,打开kc_train.csv,能够......
  • day80-todolist组件自定义事件改进
    todolist-自定义组件通过自定义组件改进todolist案例,不全使用prop方式header组件<template><divclass="todo-header"><inputtype="text"placeholder="请输......
  • vue源码分析-响应式系统(三)
    上一节,我们深入分析了以data,computed为数据创建响应式系统的过程,并对其中依赖收集和派发更新的过程进行了详细的分析。然而在使用和分析过程中依然存在或多或少的问题,这......
  • SSM框架-MyBatis学习日记4
    日志工厂如果一个数据库相关的操作出现了问题,我们可以根据输出的SQL语句快速排查问题。对于以往的开发过程,我们会经常使用到debug模式来调节,跟踪我们的代码执行过程。但......
  • java网络编程-线程池服务端
    上篇文章我们借助线程实现了服务端可以服务多个客户端,但是每次请求进来都创建新线程也是一种很大的资源消耗,线程上下文切换都会影响性能。本次我们继续对服务端进行改造,引......
  • 升级到React-Router-v6
    前言近期完成了公司新项目的开发,相关的技术栈都用到了最新版本,reactrouter也使用了v6的版本,所以借这个机会自己再梳理下reactrouterv5与v6的区别,以及v6一些新......