通过Location获取Address的使用!

Android高手进阶教程(十五)之---通过Location获取Address的使用! 大家好,上一节我讲了一下如何通过LocationManager来获取Location,没有看过上一节的同学,可以点击如下链接返回查看:Android高手进阶教程十四之---Android Location的使用!我们获取Location的目的之一肯定是有获取这个位置的详细地址,而我们有了Location在来获取Address就相对简单多了,因为GoogleApi已经封装好了方法,我们只需呀通过Location获取GeoPoint,然后在通过GeoPoint来获取我们想要的Address.下面是我做的一个简单 阅读详情


转自:http://blog.youkuaiyun.com/android_tutor/article/details/5692517

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

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

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

 

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

 

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

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

  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行代码)库,代码如下:

[xhtml] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://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>   

 

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


Android开发—通过Location获取Address Android开发—通过Location获取Address,获取手机一些信息 立即下载

相关推荐

【Linux内核】Linux内核裁剪完全指南:从理论到实战的系统优化

在嵌入式设备、专用服务器和物联网系统日益普及的今天,如何让Linux系统在资源受限的环境中高效运行成为开发者必须面对的问题。Linux内核裁剪正是解决这一问题的关键技术,它通过移除不必要的内核组件,打造出更精简、更安全、更高效的自定义操作系统。本文将系统全面地介绍Linux内核裁剪的完整流程和实用技巧,帮助您掌握这一核心技能。Linux内核裁剪是一项结合技术知识与实践经验的系统工程。明确需求:裁剪前必须清晰了解目标系统的硬件配置和功能需求。循序渐进:采取小步修改、反复测试的策略,避免一次性大幅修改。

撬动未来的支点的专栏 1388

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

大家好,上一节我讲了一下如何通过LocationManager来获取Location,没有看过上一节的同学,可以点击如下链接返回查看: Android高手进阶教程(十三)---Android Location的使用! 我们获取Location的目的之一肯定是有获取这个位置的详

Peter_hucq的专栏 959

Fast DDS源代码走读

DDS, PDP, EPD, DataWriter, DataReader

weixin_43082062的博客 713

Android根据经纬度获取位置信息

Android内部是支持这个操作的,不需要借助任何的第三方工具 主要就是Geocoder这个类。   下面是工具类全部代码。 import android.content.Context; import android.location.Address; import android.location.Geocoder; import android.util.Log; import a...

yang_niuxxx的博客 7401

InVEST模型实战指南:从水源涵养到热岛缓解的生态系统服务评估

本文提供了InVEST模型的实战指南,详细介绍了从环境搭建、数据预处理到核心模型应用的完整流程。通过水源涵养、碳存储、土壤保持和城市热岛缓解等核心模型的拆解,帮助生态学者与规划者系统量化评估生态系统服务,将复杂的生态价值转化为直观的空间数据与地图,为科学决策提供有力支撑。

weixin_29197051的博客 430

【ue4】【新手入门】|静态网格模型碰撞设置和合并Actor(合并静态网格体)

上一期学到把构建好的房子模型变成一个静态网格体(也是一个模型)但是静态网格体是没有碰撞的 所以要给这个模型设置碰撞。

m0_63501513的博客 4420

根据经纬度获取城市名

这是我自己的笔记,如有侵犯到您请留言联系 private void initGecooder() { Geocoder gc = new Geocoder(this, Locale.getDefault()); List locationList = null; try { locationList = gc.getFromLocation(strlat, strlon, 1); }

xiaxiao_new的博客 3841

QtQuick QML教程(2):qml语法介绍

​ qml一个普通的文本文件,后缀是.qml, 一个qml文件主要由两部分组成:(1) import语句 :这个就像C++中的头文件, 使用 import 导入qml需要的相关模块,可以查询Qt文档进行导入,注意每个版本的Qt模块声明可能都不同,相比于QWidget项目,QtQuick的变动是很大的;(2) QML对象树 :根对象是Window, 然后可以在Window中添加各种控件,例如按钮,文本,菜单等。

琅嬛福地 551

Android.location.Address类方法获取GPS定位信息

参考autojs的获取GPS定位的代码: function getLocationLoop(){ //判断是否已经打开GPS模块 if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {   //GPS模块打开,可以定位操作 var criteria = new Criteria(); criteria.setAccuracy(Criteria

xfxuezhang.cn 2万+

《QT+PCL 第一章》:点云读取显示

点云读取显示一、核心代码二、QT设置三、效果展示 一、核心代码 二、QT设置 三、效果展示

阁楼 1万+

【Matlab】matlab代码实现迭代学习控制

该文介绍了在Matlab中实现迭代学习控制(ILC)的方法。ILC通过利用历史轨迹数据来优化重复任务的控制性能。文章提供了一个步进电机控制示例,包括:1)定义正弦轨迹;2)设置100次迭代和0.1学习率;3)建立二阶系统模型;4)执行ILC算法计算控制输入;5)绘制期望轨迹与实际输出的对比图。该示例展示了ILC的基本实现过程,并指出实际应用可能需要更复杂的设计。代码演示了误差计算、控制输入更新和结果可视化等关键步骤。

unber的博客 201

uniapp 中APP端调用uni.getLocation偶尔获取不到address解决方法

uniapp 中APP端调用uni.getLocation偶尔获取不到address解决方法 1.配置geocode为true uni.getLocation({ type: 'wgs84', geocode:true,//设置该参数为true可直接获取经纬度及城市信息 success: function (res) { console.log('获取定位信息',res); }, fail: function (err) { conso

m0_52459016的博客 9708

Python全栈入门到实战【基础篇 05】核心基础:Python的2种运行模式(交互式+命令行式)

Python代码运行模式详解:交互式与命令行式对比 摘要: Python提供两种核心运行模式:交互式(REPL)和命令行式(脚本式)。交互式模式适合快速测试语法,即时反馈结果,通过终端输入python启动,特点是无需保存、临时执行,适合验证小段代码逻辑。命令行式模式需将代码保存为.py文件后运行,适合开发完整程序,可重复执行和修改。两种模式各有优势:交互式便于学习调试,命令行式适合实际开发。掌握两种模式的使用场景和切换方法,是Python入门的关键技能,能显著提升学习效率。

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 4559

(转)通过Location获取Address的使用!

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

gxj1680的专栏 731

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

上一篇: Android 数据库SQLiteDatabase的使用!!
下一篇: Android中万能的BaseAdapter(Spinner,ListView,GridView)的使用!
shareCode
博客等级 码龄15年 125粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值